diff --git a/README.md b/README.md index 7816191af..b28e5d6e7 100644 --- a/README.md +++ b/README.md @@ -117,7 +117,9 @@ - [Agent Social Network](#-agent-social-network) - [Configuration](#️-configuration) - [Multiple Instances](#-multiple-instances) +- [Memory](#-memory) - [CLI Reference](#-cli-reference) +- [In-Chat Commands](#-in-chat-commands) - [Python SDK](#-python-sdk) - [OpenAI-Compatible API](#-openai-compatible-api) - [Docker](#-docker) @@ -151,7 +153,12 @@ ## 📦 Install -**Install from source** (latest features, recommended for development) +> [!IMPORTANT] +> This README may describe features that are available first in the latest source code. +> If you want the newest features and experiments, install from source. +> If you want the most stable day-to-day experience, install from PyPI or with `uv`. + +**Install from source** (latest features, experimental changes may land here first; recommended for development) ```bash git clone https://github.com/HKUDS/nanobot.git @@ -159,13 +166,13 @@ cd nanobot pip install -e . ``` -**Install with [uv](https://github.com/astral-sh/uv)** (stable, fast) +**Install with [uv](https://github.com/astral-sh/uv)** (stable release, fast) ```bash uv tool install nanobot-ai ``` -**Install from PyPI** (stable) +**Install from PyPI** (stable release) ```bash pip install nanobot-ai @@ -1561,6 +1568,18 @@ nanobot gateway --config ~/.nanobot-telegram/config.json --workspace /tmp/nanobo - `--workspace` overrides the workspace defined in the config file - Cron jobs and runtime media/state are derived from the config directory +## 🧠 Memory + +nanobot uses a layered memory system designed to stay light in the moment and durable over +time. + +- `memory/history.jsonl` stores append-only summarized history +- `SOUL.md`, `USER.md`, and `memory/MEMORY.md` store long-term knowledge managed by Dream +- `Dream` runs on a schedule and can also be triggered manually +- memory changes can be inspected and restored with built-in commands + +If you want the full design, see [docs/MEMORY.md](docs/MEMORY.md). + ## 💻 CLI Reference | Command | Description | @@ -1583,6 +1602,23 @@ nanobot gateway --config ~/.nanobot-telegram/config.json --workspace /tmp/nanobo Interactive mode exits: `exit`, `quit`, `/exit`, `/quit`, `:q`, or `Ctrl+D`. +## 💬 In-Chat Commands + +These commands work inside chat channels and interactive agent sessions: + +| Command | Description | +|---------|-------------| +| `/new` | Start a new conversation | +| `/stop` | Stop the current task | +| `/restart` | Restart the bot | +| `/status` | Show bot status | +| `/dream` | Run Dream memory consolidation now | +| `/dream-log` | Show the latest Dream memory change | +| `/dream-log ` | Show a specific Dream memory change | +| `/dream-restore` | List recent Dream memory versions | +| `/dream-restore ` | Restore memory to the state before a specific change | +| `/help` | Show available in-chat commands | +
Heartbeat (Periodic Tasks) diff --git a/core_agent_lines.sh b/core_agent_lines.sh index 0891347d5..94cc854bd 100755 --- a/core_agent_lines.sh +++ b/core_agent_lines.sh @@ -1,22 +1,92 @@ #!/bin/bash -# Count core agent lines (excluding channels/, cli/, api/, providers/ adapters, -# and the high-level Python SDK facade) +set -euo pipefail + cd "$(dirname "$0")" || exit 1 -echo "nanobot core agent line count" -echo "================================" +count_top_level_py_lines() { + local dir="$1" + if [ ! -d "$dir" ]; then + echo 0 + return + fi + find "$dir" -maxdepth 1 -type f -name "*.py" -print0 | xargs -0 cat 2>/dev/null | wc -l | tr -d ' ' +} + +count_recursive_py_lines() { + local dir="$1" + if [ ! -d "$dir" ]; then + echo 0 + return + fi + find "$dir" -type f -name "*.py" -print0 | xargs -0 cat 2>/dev/null | wc -l | tr -d ' ' +} + +count_skill_lines() { + local dir="$1" + if [ ! -d "$dir" ]; then + echo 0 + return + fi + find "$dir" -type f \( -name "*.md" -o -name "*.py" -o -name "*.sh" \) -print0 | xargs -0 cat 2>/dev/null | wc -l | tr -d ' ' +} + +print_row() { + local label="$1" + local count="$2" + printf " %-16s %6s lines\n" "$label" "$count" +} + +echo "nanobot line count" +echo "==================" echo "" -for dir in agent agent/tools bus config cron heartbeat session utils; do - count=$(find "nanobot/$dir" -maxdepth 1 -name "*.py" -exec cat {} + | wc -l) - printf " %-16s %5s lines\n" "$dir/" "$count" -done +echo "Core runtime" +echo "------------" +core_agent=$(count_top_level_py_lines "nanobot/agent") +core_bus=$(count_top_level_py_lines "nanobot/bus") +core_config=$(count_top_level_py_lines "nanobot/config") +core_cron=$(count_top_level_py_lines "nanobot/cron") +core_heartbeat=$(count_top_level_py_lines "nanobot/heartbeat") +core_session=$(count_top_level_py_lines "nanobot/session") -root=$(cat nanobot/__init__.py nanobot/__main__.py | wc -l) -printf " %-16s %5s lines\n" "(root)" "$root" +print_row "agent/" "$core_agent" +print_row "bus/" "$core_bus" +print_row "config/" "$core_config" +print_row "cron/" "$core_cron" +print_row "heartbeat/" "$core_heartbeat" +print_row "session/" "$core_session" + +core_total=$((core_agent + core_bus + core_config + core_cron + core_heartbeat + core_session)) echo "" -total=$(find nanobot -name "*.py" ! -path "*/channels/*" ! -path "*/cli/*" ! -path "*/api/*" ! -path "*/command/*" ! -path "*/providers/*" ! -path "*/skills/*" ! -path "nanobot/nanobot.py" | xargs cat | wc -l) -echo " Core total: $total lines" +echo "Separate buckets" +echo "----------------" +extra_tools=$(count_recursive_py_lines "nanobot/agent/tools") +extra_skills=$(count_skill_lines "nanobot/skills") +extra_api=$(count_recursive_py_lines "nanobot/api") +extra_cli=$(count_recursive_py_lines "nanobot/cli") +extra_channels=$(count_recursive_py_lines "nanobot/channels") +extra_utils=$(count_recursive_py_lines "nanobot/utils") + +print_row "tools/" "$extra_tools" +print_row "skills/" "$extra_skills" +print_row "api/" "$extra_api" +print_row "cli/" "$extra_cli" +print_row "channels/" "$extra_channels" +print_row "utils/" "$extra_utils" + +extra_total=$((extra_tools + extra_skills + extra_api + extra_cli + extra_channels + extra_utils)) + echo "" -echo " (excludes: channels/, cli/, api/, command/, providers/, skills/, nanobot.py)" +echo "Totals" +echo "------" +print_row "core total" "$core_total" +print_row "extra total" "$extra_total" + +echo "" +echo "Notes" +echo "-----" +echo " - agent/ only counts top-level Python files under nanobot/agent" +echo " - tools/ is counted separately from nanobot/agent/tools" +echo " - skills/ counts .md, .py, and .sh files" +echo " - not included here: command/, providers/, security/, templates/, nanobot.py, root files" diff --git a/docs/MEMORY.md b/docs/MEMORY.md new file mode 100644 index 000000000..414fcdca6 --- /dev/null +++ b/docs/MEMORY.md @@ -0,0 +1,191 @@ +# Memory in nanobot + +> **Note:** This design is currently an experiment in the latest source code version and is planned to officially ship in `v0.1.5`. + +nanobot's memory is built on a simple belief: memory should feel alive, but it should not feel chaotic. + +Good memory is not a pile of notes. It is a quiet system of attention. It notices what is worth keeping, lets go of what no longer needs the spotlight, and turns lived experience into something calm, durable, and useful. + +That is the shape of memory in nanobot. + +## The Design + +nanobot does not treat memory as one giant file. + +It separates memory into layers, because different kinds of remembering deserve different tools: + +- `session.messages` holds the living short-term conversation. +- `memory/history.jsonl` is the running archive of compressed past turns. +- `SOUL.md`, `USER.md`, and `memory/MEMORY.md` are the durable knowledge files. +- `GitStore` records how those durable files change over time. + +This keeps the system light in the moment, but reflective over time. + +## The Flow + +Memory moves through nanobot in two stages. + +### Stage 1: Consolidator + +When a conversation grows large enough to pressure the context window, nanobot does not try to carry every old message forever. + +Instead, the `Consolidator` summarizes the oldest safe slice of the conversation and appends that summary to `memory/history.jsonl`. + +This file is: + +- append-only +- cursor-based +- optimized for machine consumption first, human inspection second + +Each line is a JSON object: + +```json +{"cursor": 42, "timestamp": "2026-04-03 00:02", "content": "- User prefers dark mode\n- Decided to use PostgreSQL"} +``` + +It is not the final memory. It is the material from which final memory is shaped. + +### Stage 2: Dream + +`Dream` is the slower, more thoughtful layer. It runs on a cron schedule by default and can also be triggered manually. + +Dream reads: + +- new entries from `memory/history.jsonl` +- the current `SOUL.md` +- the current `USER.md` +- the current `memory/MEMORY.md` + +Then it works in two phases: + +1. It studies what is new and what is already known. +2. It edits the long-term files surgically, not by rewriting everything, but by making the smallest honest change that keeps memory coherent. + +This is why nanobot's memory is not just archival. It is interpretive. + +## The Files + +``` +workspace/ +├── SOUL.md # The bot's long-term voice and communication style +├── USER.md # Stable knowledge about the user +└── memory/ + ├── MEMORY.md # Project facts, decisions, and durable context + ├── history.jsonl # Append-only history summaries + ├── .cursor # Consolidator write cursor + ├── .dream_cursor # Dream consumption cursor + └── .git/ # Version history for long-term memory files +``` + +These files play different roles: + +- `SOUL.md` remembers how nanobot should sound. +- `USER.md` remembers who the user is and what they prefer. +- `MEMORY.md` remembers what remains true about the work itself. +- `history.jsonl` remembers what happened on the way there. + +## Why `history.jsonl` + +The old `HISTORY.md` format was pleasant for casual reading, but it was too fragile as an operational substrate. + +`history.jsonl` gives nanobot: + +- stable incremental cursors +- safer machine parsing +- easier batching +- cleaner migration and compaction +- a better boundary between raw history and curated knowledge + +You can still search it with familiar tools: + +```bash +# grep +grep -i "keyword" memory/history.jsonl + +# jq +cat memory/history.jsonl | jq -r 'select(.content | test("keyword"; "i")) | .content' | tail -20 + +# Python +python -c "import json; [print(json.loads(l).get('content','')) for l in open('memory/history.jsonl','r',encoding='utf-8') if l.strip() and 'keyword' in l.lower()][-20:]" +``` + +The difference is philosophical as much as technical: + +- `history.jsonl` is for structure +- `SOUL.md`, `USER.md`, and `MEMORY.md` are for meaning + +## Commands + +Memory is not hidden behind the curtain. Users can inspect and guide it. + +| Command | What it does | +|---------|--------------| +| `/dream` | Run Dream immediately | +| `/dream-log` | Show the latest Dream memory change | +| `/dream-log ` | Show a specific Dream change | +| `/dream-restore` | List recent Dream memory versions | +| `/dream-restore ` | Restore memory to the state before a specific change | + +These commands exist for a reason: automatic memory is powerful, but users should always retain the right to inspect, understand, and restore it. + +## Versioned Memory + +After Dream changes long-term memory files, nanobot can record that change with `GitStore`. + +This gives memory a history of its own: + +- you can inspect what changed +- you can compare versions +- you can restore a previous state + +That turns memory from a silent mutation into an auditable process. + +## Configuration + +Dream is configured under `agents.defaults.dream`: + +```json +{ + "agents": { + "defaults": { + "dream": { + "intervalH": 2, + "modelOverride": null, + "maxBatchSize": 20, + "maxIterations": 10 + } + } + } +} +``` + +| Field | Meaning | +|-------|---------| +| `intervalH` | How often Dream runs, in hours | +| `modelOverride` | Optional Dream-specific model override | +| `maxBatchSize` | How many history entries Dream processes per run | +| `maxIterations` | The tool budget for Dream's editing phase | + +In practical terms: + +- `modelOverride: null` means Dream uses the same model as the main agent. Set it only if you want Dream to run on a different model. +- `maxBatchSize` controls how many new `history.jsonl` entries Dream consumes in one run. Larger batches catch up faster; smaller batches are lighter and steadier. +- `maxIterations` limits how many read/edit steps Dream can take while updating `SOUL.md`, `USER.md`, and `MEMORY.md`. It is a safety budget, not a quality score. +- `intervalH` is the normal way to configure Dream. Internally it runs as an `every` schedule, not as a cron expression. + +Legacy note: + +- Older source-based configs may still contain `dream.cron`. nanobot continues to honor it for backward compatibility, but new configs should use `intervalH`. +- Older source-based configs may still contain `dream.model`. nanobot continues to honor it for backward compatibility, but new configs should use `modelOverride`. + +## In Practice + +What this means in daily use is simple: + +- conversations can stay fast without carrying infinite context +- durable facts can become clearer over time instead of noisier +- the user can inspect and restore memory when needed + +Memory should not feel like a dump. It should feel like continuity. + +That is what this design is trying to protect. diff --git a/docs/PYTHON_SDK.md b/docs/PYTHON_SDK.md index 357722e5e..2b51055a9 100644 --- a/docs/PYTHON_SDK.md +++ b/docs/PYTHON_SDK.md @@ -1,5 +1,7 @@ # Python SDK +> **Note:** This interface is currently an experiment in the latest source code version and is planned to officially ship in `v0.1.5`. + Use nanobot programmatically — load config, run the agent, get results. ## Quick Start diff --git a/nanobot/agent/__init__.py b/nanobot/agent/__init__.py index 7d3ab2af4..a8805a3ad 100644 --- a/nanobot/agent/__init__.py +++ b/nanobot/agent/__init__.py @@ -3,7 +3,7 @@ from nanobot.agent.context import ContextBuilder from nanobot.agent.hook import AgentHook, AgentHookContext, CompositeHook from nanobot.agent.loop import AgentLoop -from nanobot.agent.memory import MemoryStore +from nanobot.agent.memory import Consolidator, Dream, MemoryStore from nanobot.agent.skills import SkillsLoader from nanobot.agent.subagent import SubagentManager @@ -13,6 +13,7 @@ __all__ = [ "AgentLoop", "CompositeHook", "ContextBuilder", + "Dream", "MemoryStore", "SkillsLoader", "SubagentManager", diff --git a/nanobot/agent/context.py b/nanobot/agent/context.py index 8ce2873a9..63ce35632 100644 --- a/nanobot/agent/context.py +++ b/nanobot/agent/context.py @@ -82,8 +82,8 @@ You are nanobot, a helpful AI assistant. ## Workspace Your workspace is at: {workspace_path} -- Long-term memory: {workspace_path}/memory/MEMORY.md (write important facts here) -- History log: {workspace_path}/memory/HISTORY.md (grep-searchable). Each entry starts with [YYYY-MM-DD HH:MM]. +- Long-term memory: {workspace_path}/memory/MEMORY.md (automatically managed by Dream — do not edit directly) +- History log: {workspace_path}/memory/history.jsonl (append-only JSONL, not grep-searchable). - Custom skills: {workspace_path}/skills/{{skill-name}}/SKILL.md {platform_policy} diff --git a/nanobot/agent/loop.py b/nanobot/agent/loop.py index 48a68cdcd..0dc41c2f7 100644 --- a/nanobot/agent/loop.py +++ b/nanobot/agent/loop.py @@ -15,7 +15,7 @@ from loguru import logger from nanobot.agent.context import ContextBuilder from nanobot.agent.hook import AgentHook, AgentHookContext, CompositeHook -from nanobot.agent.memory import MemoryConsolidator +from nanobot.agent.memory import Consolidator, Dream from nanobot.agent.runner import AgentRunSpec, AgentRunner from nanobot.agent.subagent import SubagentManager from nanobot.agent.tools.cron import CronTool @@ -240,8 +240,8 @@ class AgentLoop: self._concurrency_gate: asyncio.Semaphore | None = ( asyncio.Semaphore(_max) if _max > 0 else None ) - self.memory_consolidator = MemoryConsolidator( - workspace=workspace, + self.consolidator = Consolidator( + store=self.context.memory, provider=provider, model=self.model, sessions=self.sessions, @@ -250,6 +250,11 @@ class AgentLoop: get_tool_definitions=self.tools.get_definitions, max_completion_tokens=provider.generation.max_tokens, ) + self.dream = Dream( + store=self.context.memory, + provider=provider, + model=self.model, + ) self._register_default_tools() self.commands = CommandRouter() register_builtin_commands(self.commands) @@ -520,7 +525,7 @@ class AgentLoop: session = self.sessions.get_or_create(key) if self._restore_runtime_checkpoint(session): self.sessions.save(session) - await self.memory_consolidator.maybe_consolidate_by_tokens(session) + await self.consolidator.maybe_consolidate_by_tokens(session) self._set_tool_context(channel, chat_id, msg.metadata.get("message_id")) history = session.get_history(max_messages=0) current_role = "assistant" if msg.sender_id == "subagent" else "user" @@ -536,7 +541,7 @@ class AgentLoop: self._save_turn(session, all_msgs, 1 + len(history)) self._clear_runtime_checkpoint(session) self.sessions.save(session) - self._schedule_background(self.memory_consolidator.maybe_consolidate_by_tokens(session)) + self._schedule_background(self.consolidator.maybe_consolidate_by_tokens(session)) return OutboundMessage(channel=channel, chat_id=chat_id, content=final_content or "Background task completed.") @@ -554,7 +559,7 @@ class AgentLoop: if result := await self.commands.dispatch(ctx): return result - await self.memory_consolidator.maybe_consolidate_by_tokens(session) + await self.consolidator.maybe_consolidate_by_tokens(session) self._set_tool_context(msg.channel, msg.chat_id, msg.metadata.get("message_id")) if message_tool := self.tools.get("message"): @@ -593,7 +598,7 @@ class AgentLoop: self._save_turn(session, all_msgs, 1 + len(history)) self._clear_runtime_checkpoint(session) self.sessions.save(session) - self._schedule_background(self.memory_consolidator.maybe_consolidate_by_tokens(session)) + self._schedule_background(self.consolidator.maybe_consolidate_by_tokens(session)) if (mt := self.tools.get("message")) and isinstance(mt, MessageTool) and mt._sent_in_turn: return None diff --git a/nanobot/agent/memory.py b/nanobot/agent/memory.py index aa2de9290..3fbc651c9 100644 --- a/nanobot/agent/memory.py +++ b/nanobot/agent/memory.py @@ -1,9 +1,10 @@ -"""Memory system for persistent agent memory.""" +"""Memory system: pure file I/O store, lightweight Consolidator, and Dream processor.""" from __future__ import annotations import asyncio import json +import re import weakref from datetime import datetime from pathlib import Path @@ -11,94 +12,307 @@ from typing import TYPE_CHECKING, Any, Callable from loguru import logger -from nanobot.utils.helpers import ensure_dir, estimate_message_tokens, estimate_prompt_tokens_chain +from nanobot.utils.helpers import ensure_dir, estimate_message_tokens, estimate_prompt_tokens_chain, strip_think + +from nanobot.agent.runner import AgentRunSpec, AgentRunner +from nanobot.agent.tools.registry import ToolRegistry +from nanobot.utils.gitstore import GitStore if TYPE_CHECKING: from nanobot.providers.base import LLMProvider from nanobot.session.manager import Session, SessionManager -_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 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"], - }, - }, - } -] - - -def _ensure_text(value: Any) -> str: - """Normalize tool-call payload values to text for file storage.""" - return value if isinstance(value, str) else json.dumps(value, ensure_ascii=False) - - -def _normalize_save_memory_args(args: Any) -> dict[str, Any] | None: - """Normalize provider tool-call arguments to the expected dict shape.""" - if isinstance(args, str): - args = json.loads(args) - if isinstance(args, list): - return args[0] if args and isinstance(args[0], dict) else None - return args if isinstance(args, dict) else None - -_TOOL_CHOICE_ERROR_MARKERS = ( - "tool_choice", - "toolchoice", - "does not support", - 'should be ["none", "auto"]', -) - - -def _is_tool_choice_unsupported(content: str | None) -> bool: - """Detect provider errors caused by forced tool_choice being unsupported.""" - text = (content or "").lower() - return any(m in text for m in _TOOL_CHOICE_ERROR_MARKERS) - +# --------------------------------------------------------------------------- +# MemoryStore — pure file I/O layer +# --------------------------------------------------------------------------- class MemoryStore: - """Two-layer memory: MEMORY.md (long-term facts) + HISTORY.md (grep-searchable log).""" + """Pure file I/O for memory files: MEMORY.md, history.jsonl, SOUL.md, USER.md.""" - _MAX_FAILURES_BEFORE_RAW_ARCHIVE = 3 + _DEFAULT_MAX_HISTORY = 1000 + _LEGACY_ENTRY_START_RE = re.compile(r"^\[(\d{4}-\d{2}-\d{2}[^\]]*)\]\s*") + _LEGACY_TIMESTAMP_RE = re.compile(r"^\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2})\]\s*") + _LEGACY_RAW_MESSAGE_RE = re.compile( + r"^\[\d{4}-\d{2}-\d{2}[^\]]*\]\s+[A-Z][A-Z0-9_]*(?:\s+\[tools:\s*[^\]]+\])?:" + ) - def __init__(self, workspace: Path): + def __init__(self, workspace: Path, max_history_entries: int = _DEFAULT_MAX_HISTORY): + self.workspace = workspace + self.max_history_entries = max_history_entries self.memory_dir = ensure_dir(workspace / "memory") self.memory_file = self.memory_dir / "MEMORY.md" - self.history_file = self.memory_dir / "HISTORY.md" - self._consecutive_failures = 0 + self.history_file = self.memory_dir / "history.jsonl" + self.legacy_history_file = self.memory_dir / "HISTORY.md" + self.soul_file = workspace / "SOUL.md" + self.user_file = workspace / "USER.md" + self._cursor_file = self.memory_dir / ".cursor" + self._dream_cursor_file = self.memory_dir / ".dream_cursor" + self._git = GitStore(workspace, tracked_files=[ + "SOUL.md", "USER.md", "memory/MEMORY.md", + ]) + self._maybe_migrate_legacy_history() - def read_long_term(self) -> str: - if self.memory_file.exists(): - return self.memory_file.read_text(encoding="utf-8") - return "" + @property + def git(self) -> GitStore: + return self._git - def write_long_term(self, content: str) -> None: + # -- generic helpers ----------------------------------------------------- + + @staticmethod + def read_file(path: Path) -> str: + try: + return path.read_text(encoding="utf-8") + except FileNotFoundError: + return "" + + def _maybe_migrate_legacy_history(self) -> None: + """One-time upgrade from legacy HISTORY.md to history.jsonl. + + The migration is best-effort and prioritizes preserving as much content + as possible over perfect parsing. + """ + if not self.legacy_history_file.exists(): + return + if self.history_file.exists() and self.history_file.stat().st_size > 0: + return + + try: + legacy_text = self.legacy_history_file.read_text( + encoding="utf-8", + errors="replace", + ) + except OSError: + logger.exception("Failed to read legacy HISTORY.md for migration") + return + + entries = self._parse_legacy_history(legacy_text) + try: + if entries: + self._write_entries(entries) + last_cursor = entries[-1]["cursor"] + self._cursor_file.write_text(str(last_cursor), encoding="utf-8") + # Default to "already processed" so upgrades do not replay the + # user's entire historical archive into Dream on first start. + self._dream_cursor_file.write_text(str(last_cursor), encoding="utf-8") + + backup_path = self._next_legacy_backup_path() + self.legacy_history_file.replace(backup_path) + logger.info( + "Migrated legacy HISTORY.md to history.jsonl ({} entries)", + len(entries), + ) + except Exception: + logger.exception("Failed to migrate legacy HISTORY.md") + + def _parse_legacy_history(self, text: str) -> list[dict[str, Any]]: + normalized = text.replace("\r\n", "\n").replace("\r", "\n").strip() + if not normalized: + return [] + + fallback_timestamp = self._legacy_fallback_timestamp() + entries: list[dict[str, Any]] = [] + chunks = self._split_legacy_history_chunks(normalized) + + for cursor, chunk in enumerate(chunks, start=1): + timestamp = fallback_timestamp + content = chunk + match = self._LEGACY_TIMESTAMP_RE.match(chunk) + if match: + timestamp = match.group(1) + remainder = chunk[match.end():].lstrip() + if remainder: + content = remainder + + entries.append({ + "cursor": cursor, + "timestamp": timestamp, + "content": content, + }) + return entries + + def _split_legacy_history_chunks(self, text: str) -> list[str]: + lines = text.split("\n") + chunks: list[str] = [] + current: list[str] = [] + saw_blank_separator = False + + for line in lines: + if saw_blank_separator and line.strip() and current: + chunks.append("\n".join(current).strip()) + current = [line] + saw_blank_separator = False + continue + if self._should_start_new_legacy_chunk(line, current): + chunks.append("\n".join(current).strip()) + current = [line] + saw_blank_separator = False + continue + current.append(line) + saw_blank_separator = not line.strip() + + if current: + chunks.append("\n".join(current).strip()) + return [chunk for chunk in chunks if chunk] + + def _should_start_new_legacy_chunk(self, line: str, current: list[str]) -> bool: + if not current: + return False + if not self._LEGACY_ENTRY_START_RE.match(line): + return False + if self._is_raw_legacy_chunk(current) and self._LEGACY_RAW_MESSAGE_RE.match(line): + return False + return True + + def _is_raw_legacy_chunk(self, lines: list[str]) -> bool: + first_nonempty = next((line for line in lines if line.strip()), "") + match = self._LEGACY_TIMESTAMP_RE.match(first_nonempty) + if not match: + return False + return first_nonempty[match.end():].lstrip().startswith("[RAW]") + + def _legacy_fallback_timestamp(self) -> str: + try: + return datetime.fromtimestamp( + self.legacy_history_file.stat().st_mtime, + ).strftime("%Y-%m-%d %H:%M") + except OSError: + return datetime.now().strftime("%Y-%m-%d %H:%M") + + def _next_legacy_backup_path(self) -> Path: + candidate = self.memory_dir / "HISTORY.md.bak" + suffix = 2 + while candidate.exists(): + candidate = self.memory_dir / f"HISTORY.md.bak.{suffix}" + suffix += 1 + return candidate + + # -- MEMORY.md (long-term facts) ----------------------------------------- + + def read_memory(self) -> str: + return self.read_file(self.memory_file) + + def write_memory(self, content: str) -> None: self.memory_file.write_text(content, encoding="utf-8") - def append_history(self, entry: str) -> None: - with open(self.history_file, "a", encoding="utf-8") as f: - f.write(entry.rstrip() + "\n\n") + # -- SOUL.md ------------------------------------------------------------- + + def read_soul(self) -> str: + return self.read_file(self.soul_file) + + def write_soul(self, content: str) -> None: + self.soul_file.write_text(content, encoding="utf-8") + + # -- USER.md ------------------------------------------------------------- + + def read_user(self) -> str: + return self.read_file(self.user_file) + + def write_user(self, content: str) -> None: + self.user_file.write_text(content, encoding="utf-8") + + # -- context injection (used by context.py) ------------------------------ def get_memory_context(self) -> str: - long_term = self.read_long_term() + long_term = self.read_memory() return f"## Long-term Memory\n{long_term}" if long_term else "" + # -- history.jsonl — append-only, JSONL format --------------------------- + + def append_history(self, entry: str) -> int: + """Append *entry* to history.jsonl and return its auto-incrementing cursor.""" + cursor = self._next_cursor() + ts = datetime.now().strftime("%Y-%m-%d %H:%M") + record = {"cursor": cursor, "timestamp": ts, "content": strip_think(entry.rstrip()) or entry.rstrip()} + with open(self.history_file, "a", encoding="utf-8") as f: + f.write(json.dumps(record, ensure_ascii=False) + "\n") + self._cursor_file.write_text(str(cursor), encoding="utf-8") + return cursor + + def _next_cursor(self) -> int: + """Read the current cursor counter and return next value.""" + if self._cursor_file.exists(): + try: + return int(self._cursor_file.read_text(encoding="utf-8").strip()) + 1 + except (ValueError, OSError): + pass + # Fallback: read last line's cursor from the JSONL file. + last = self._read_last_entry() + if last: + return last["cursor"] + 1 + return 1 + + def read_unprocessed_history(self, since_cursor: int) -> list[dict[str, Any]]: + """Return history entries with cursor > *since_cursor*.""" + return [e for e in self._read_entries() if e["cursor"] > since_cursor] + + def compact_history(self) -> None: + """Drop oldest entries if the file exceeds *max_history_entries*.""" + if self.max_history_entries <= 0: + return + entries = self._read_entries() + if len(entries) <= self.max_history_entries: + return + kept = entries[-self.max_history_entries:] + self._write_entries(kept) + + # -- JSONL helpers ------------------------------------------------------- + + def _read_entries(self) -> list[dict[str, Any]]: + """Read all entries from history.jsonl.""" + entries: list[dict[str, Any]] = [] + try: + with open(self.history_file, "r", encoding="utf-8") as f: + for line in f: + line = line.strip() + if line: + try: + entries.append(json.loads(line)) + except json.JSONDecodeError: + continue + except FileNotFoundError: + pass + return entries + + def _read_last_entry(self) -> dict[str, Any] | None: + """Read the last entry from the JSONL file efficiently.""" + try: + with open(self.history_file, "rb") as f: + f.seek(0, 2) + size = f.tell() + if size == 0: + return None + read_size = min(size, 4096) + f.seek(size - read_size) + data = f.read().decode("utf-8") + lines = [l for l in data.split("\n") if l.strip()] + if not lines: + return None + return json.loads(lines[-1]) + except (FileNotFoundError, json.JSONDecodeError): + return None + + def _write_entries(self, entries: list[dict[str, Any]]) -> None: + """Overwrite history.jsonl with the given entries.""" + with open(self.history_file, "w", encoding="utf-8") as f: + for entry in entries: + f.write(json.dumps(entry, ensure_ascii=False) + "\n") + + # -- dream cursor -------------------------------------------------------- + + def get_last_dream_cursor(self) -> int: + if self._dream_cursor_file.exists(): + try: + return int(self._dream_cursor_file.read_text(encoding="utf-8").strip()) + except (ValueError, OSError): + pass + return 0 + + def set_last_dream_cursor(self, cursor: int) -> None: + self._dream_cursor_file.write_text(str(cursor), encoding="utf-8") + + # -- message formatting utility ------------------------------------------ + @staticmethod def _format_messages(messages: list[dict]) -> str: lines = [] @@ -111,107 +325,10 @@ class MemoryStore: ) return "\n".join(lines) - async def consolidate( - self, - messages: list[dict], - provider: LLMProvider, - model: str, - ) -> bool: - """Consolidate the provided message chunk into MEMORY.md + HISTORY.md.""" - if not messages: - return True - - 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 -{self._format_messages(messages)}""" - - chat_messages = [ - {"role": "system", "content": "You are a memory consolidation agent. Call the save_memory tool with your consolidation of the conversation."}, - {"role": "user", "content": prompt}, - ] - - try: - forced = {"type": "function", "function": {"name": "save_memory"}} - response = await provider.chat_with_retry( - messages=chat_messages, - tools=_SAVE_MEMORY_TOOL, - model=model, - tool_choice=forced, - ) - - if response.finish_reason == "error" and _is_tool_choice_unsupported( - response.content - ): - logger.warning("Forced tool_choice unsupported, retrying with auto") - response = await provider.chat_with_retry( - messages=chat_messages, - tools=_SAVE_MEMORY_TOOL, - model=model, - tool_choice="auto", - ) - - if not response.has_tool_calls: - logger.warning( - "Memory consolidation: LLM did not call save_memory " - "(finish_reason={}, content_len={}, content_preview={})", - response.finish_reason, - len(response.content or ""), - (response.content or "")[:200], - ) - return self._fail_or_raw_archive(messages) - - args = _normalize_save_memory_args(response.tool_calls[0].arguments) - if args is None: - logger.warning("Memory consolidation: unexpected save_memory arguments") - return self._fail_or_raw_archive(messages) - - if "history_entry" not in args or "memory_update" not in args: - logger.warning("Memory consolidation: save_memory payload missing required fields") - return self._fail_or_raw_archive(messages) - - entry = args["history_entry"] - update = args["memory_update"] - - if entry is None or update is None: - logger.warning("Memory consolidation: save_memory payload contains null required fields") - return self._fail_or_raw_archive(messages) - - entry = _ensure_text(entry).strip() - if not entry: - logger.warning("Memory consolidation: history_entry is empty after normalization") - return self._fail_or_raw_archive(messages) - - self.append_history(entry) - update = _ensure_text(update) - if update != current_memory: - self.write_long_term(update) - - self._consecutive_failures = 0 - logger.info("Memory consolidation done for {} messages", len(messages)) - return True - except Exception: - logger.exception("Memory consolidation failed") - return self._fail_or_raw_archive(messages) - - def _fail_or_raw_archive(self, messages: list[dict]) -> bool: - """Increment failure count; after threshold, raw-archive messages and return True.""" - self._consecutive_failures += 1 - if self._consecutive_failures < self._MAX_FAILURES_BEFORE_RAW_ARCHIVE: - return False - self._raw_archive(messages) - self._consecutive_failures = 0 - return True - - def _raw_archive(self, messages: list[dict]) -> None: + def raw_archive(self, messages: list[dict]) -> None: """Fallback: dump raw messages to HISTORY.md without LLM summarization.""" - ts = datetime.now().strftime("%Y-%m-%d %H:%M") self.append_history( - f"[{ts}] [RAW] {len(messages)} messages\n" + f"[RAW] {len(messages)} messages\n" f"{self._format_messages(messages)}" ) logger.warning( @@ -219,8 +336,14 @@ class MemoryStore: ) -class MemoryConsolidator: - """Owns consolidation policy, locking, and session offset updates.""" + +# --------------------------------------------------------------------------- +# Consolidator — lightweight token-budget triggered consolidation +# --------------------------------------------------------------------------- + + +class Consolidator: + """Lightweight consolidation: summarizes evicted messages, appends to HISTORY.md.""" _MAX_CONSOLIDATION_ROUNDS = 5 @@ -228,7 +351,7 @@ class MemoryConsolidator: def __init__( self, - workspace: Path, + store: MemoryStore, provider: LLMProvider, model: str, sessions: SessionManager, @@ -237,7 +360,7 @@ class MemoryConsolidator: get_tool_definitions: Callable[[], list[dict[str, Any]]], max_completion_tokens: int = 4096, ): - self.store = MemoryStore(workspace) + self.store = store self.provider = provider self.model = model self.sessions = sessions @@ -245,16 +368,14 @@ class MemoryConsolidator: self.max_completion_tokens = max_completion_tokens self._build_messages = build_messages self._get_tool_definitions = get_tool_definitions - self._locks: weakref.WeakValueDictionary[str, asyncio.Lock] = weakref.WeakValueDictionary() + self._locks: weakref.WeakValueDictionary[str, asyncio.Lock] = ( + weakref.WeakValueDictionary() + ) def get_lock(self, session_key: str) -> asyncio.Lock: """Return the shared consolidation lock for one session.""" return self._locks.setdefault(session_key, asyncio.Lock()) - async def consolidate_messages(self, messages: list[dict[str, object]]) -> bool: - """Archive a selected message chunk into persistent memory.""" - return await self.store.consolidate(messages, self.provider, self.model) - def pick_consolidation_boundary( self, session: Session, @@ -294,14 +415,50 @@ class MemoryConsolidator: self._get_tool_definitions(), ) - async def archive_messages(self, messages: list[dict[str, object]]) -> bool: - """Archive messages with guaranteed persistence (retries until raw-dump fallback).""" + async def archive(self, messages: list[dict]) -> bool: + """Summarize messages via LLM and append to HISTORY.md. + + Returns True on success (or degraded success), False if nothing to do. + """ if not messages: + return False + try: + formatted = MemoryStore._format_messages(messages) + response = await self.provider.chat_with_retry( + model=self.model, + messages=[ + { + "role": "system", + "content": ( + "Extract key facts from this conversation. " + "Only output items matching these categories, skip everything else:\n" + "- User facts: personal info, preferences, stated opinions, habits\n" + "- Decisions: choices made, conclusions reached\n" + "- Solutions: working approaches discovered through trial and error, " + "especially non-obvious methods that succeeded after failed attempts\n" + "- Events: plans, deadlines, notable occurrences\n" + "- Preferences: communication style, tool preferences\n\n" + "Priority: user corrections and preferences > solutions > decisions > events > environment facts. " + "The most valuable memory prevents the user from having to repeat themselves.\n\n" + "Skip: code patterns derivable from source, git history, " + "or anything already captured in existing memory.\n\n" + "Output as concise bullet points, one fact per line. " + "No preamble, no commentary.\n" + "If nothing noteworthy happened, output: (nothing)" + ), + }, + {"role": "user", "content": formatted}, + ], + tools=None, + tool_choice=None, + ) + summary = response.content or "[no summary]" + self.store.append_history(summary) + return True + except Exception: + logger.warning("Consolidation LLM call failed, raw-dumping to history") + self.store.raw_archive(messages) return True - for _ in range(self.store._MAX_FAILURES_BEFORE_RAW_ARCHIVE): - if await self.consolidate_messages(messages): - return True - return True async def maybe_consolidate_by_tokens(self, session: Session) -> None: """Loop: archive old messages until prompt fits within safe budget. @@ -356,7 +513,7 @@ class MemoryConsolidator: source, len(chunk), ) - if not await self.consolidate_messages(chunk): + if not await self.archive(chunk): return session.last_consolidated = end_idx self.sessions.save(session) @@ -364,3 +521,186 @@ class MemoryConsolidator: estimated, source = self.estimate_session_prompt_tokens(session) if estimated <= 0: return + + +# --------------------------------------------------------------------------- +# Dream — heavyweight cron-scheduled memory consolidation +# --------------------------------------------------------------------------- + + +class Dream: + """Two-phase memory processor: analyze HISTORY.md, then edit files via AgentRunner. + + Phase 1 produces an analysis summary (plain LLM call). + Phase 2 delegates to AgentRunner with read_file / edit_file tools so the + LLM can make targeted, incremental edits instead of replacing entire files. + """ + + _PHASE1_SYSTEM = ( + "Compare conversation history against current memory files. " + "Output one line per finding:\n" + "[FILE] atomic fact or change description\n\n" + "Files: USER (identity, preferences, habits), " + "SOUL (bot behavior, tone), " + "MEMORY (knowledge, project context, tool patterns)\n\n" + "Rules:\n" + "- Only new or conflicting information — skip duplicates and ephemera\n" + "- Prefer atomic facts: \"has a cat named Luna\" not \"discussed pet care\"\n" + "- Corrections: [USER] location is Tokyo, not Osaka\n" + "- Also capture confirmed approaches: if the user validated a non-obvious choice, note it\n\n" + "If nothing needs updating: [SKIP] no new information" + ) + + _PHASE2_SYSTEM = ( + "Update memory files based on the analysis below.\n\n" + "## Quality standards\n" + "- Every line must carry standalone value — no filler\n" + "- Concise bullet points under clear headers\n" + "- Remove outdated or contradicted information\n\n" + "## Editing\n" + "- File contents provided below — edit directly, no read_file needed\n" + "- Batch changes to the same file into one edit_file call\n" + "- Surgical edits only — never rewrite entire files\n" + "- Do NOT overwrite correct entries — only add, update, or remove\n" + "- If nothing to update, stop without calling tools" + ) + + def __init__( + self, + store: MemoryStore, + provider: LLMProvider, + model: str, + max_batch_size: int = 20, + max_iterations: int = 10, + max_tool_result_chars: int = 16_000, + ): + self.store = store + self.provider = provider + self.model = model + self.max_batch_size = max_batch_size + self.max_iterations = max_iterations + self.max_tool_result_chars = max_tool_result_chars + self._runner = AgentRunner(provider) + self._tools = self._build_tools() + + # -- tool registry ------------------------------------------------------- + + def _build_tools(self) -> ToolRegistry: + """Build a minimal tool registry for the Dream agent.""" + from nanobot.agent.tools.filesystem import EditFileTool, ReadFileTool + + tools = ToolRegistry() + workspace = self.store.workspace + tools.register(ReadFileTool(workspace=workspace, allowed_dir=workspace)) + tools.register(EditFileTool(workspace=workspace, allowed_dir=workspace)) + return tools + + # -- main entry ---------------------------------------------------------- + + async def run(self) -> bool: + """Process unprocessed history entries. Returns True if work was done.""" + last_cursor = self.store.get_last_dream_cursor() + entries = self.store.read_unprocessed_history(since_cursor=last_cursor) + if not entries: + return False + + batch = entries[: self.max_batch_size] + logger.info( + "Dream: processing {} entries (cursor {}→{}), batch={}", + len(entries), last_cursor, batch[-1]["cursor"], len(batch), + ) + + # Build history text for LLM + history_text = "\n".join( + f"[{e['timestamp']}] {e['content']}" for e in batch + ) + + # Current file contents + current_memory = self.store.read_memory() or "(empty)" + current_soul = self.store.read_soul() or "(empty)" + current_user = self.store.read_user() or "(empty)" + file_context = ( + f"## Current MEMORY.md\n{current_memory}\n\n" + f"## Current SOUL.md\n{current_soul}\n\n" + f"## Current USER.md\n{current_user}" + ) + + # Phase 1: Analyze + phase1_prompt = ( + f"## Conversation History\n{history_text}\n\n{file_context}" + ) + + try: + phase1_response = await self.provider.chat_with_retry( + model=self.model, + messages=[ + {"role": "system", "content": self._PHASE1_SYSTEM}, + {"role": "user", "content": phase1_prompt}, + ], + tools=None, + tool_choice=None, + ) + analysis = phase1_response.content or "" + logger.debug("Dream Phase 1 complete ({} chars)", len(analysis)) + except Exception: + logger.exception("Dream Phase 1 failed") + return False + + # Phase 2: Delegate to AgentRunner with read_file / edit_file + phase2_prompt = f"## Analysis Result\n{analysis}\n\n{file_context}" + + tools = self._tools + messages: list[dict[str, Any]] = [ + {"role": "system", "content": self._PHASE2_SYSTEM}, + {"role": "user", "content": phase2_prompt}, + ] + + try: + result = await self._runner.run(AgentRunSpec( + initial_messages=messages, + tools=tools, + model=self.model, + max_iterations=self.max_iterations, + max_tool_result_chars=self.max_tool_result_chars, + fail_on_tool_error=True, + )) + logger.debug( + "Dream Phase 2 complete: stop_reason={}, tool_events={}", + result.stop_reason, len(result.tool_events), + ) + except Exception: + logger.exception("Dream Phase 2 failed") + result = None + + # Build changelog from tool events + changelog: list[str] = [] + if result and result.tool_events: + for event in result.tool_events: + if event["status"] == "ok": + changelog.append(f"{event['name']}: {event['detail']}") + + # Advance cursor — always, to avoid re-processing Phase 1 + new_cursor = batch[-1]["cursor"] + self.store.set_last_dream_cursor(new_cursor) + self.store.compact_history() + + if result and result.stop_reason == "completed": + logger.info( + "Dream done: {} change(s), cursor advanced to {}", + len(changelog), new_cursor, + ) + else: + reason = result.stop_reason if result else "exception" + logger.warning( + "Dream incomplete ({}): cursor advanced to {}", + reason, new_cursor, + ) + + # Git auto-commit (only when there are actual changes) + if changelog and self.store.git.is_initialized(): + ts = batch[-1]["timestamp"] + sha = self.store.git.auto_commit(f"dream: {ts}, {len(changelog)} change(s)") + if sha: + logger.info("Dream commit: {}", sha) + + return True diff --git a/nanobot/agent/tools/cron.py b/nanobot/agent/tools/cron.py index f2aba0b97..ada55d7cf 100644 --- a/nanobot/agent/tools/cron.py +++ b/nanobot/agent/tools/cron.py @@ -6,7 +6,7 @@ from typing import Any from nanobot.agent.tools.base import Tool from nanobot.cron.service import CronService -from nanobot.cron.types import CronJobState, CronSchedule +from nanobot.cron.types import CronJob, CronJobState, CronSchedule class CronTool(Tool): @@ -219,6 +219,12 @@ class CronTool(Tool): lines.append(f" Next run: {self._format_timestamp(state.next_run_at_ms, display_tz)}") return lines + @staticmethod + def _system_job_purpose(job: CronJob) -> str: + if job.name == "dream": + return "Dream memory consolidation for long-term memory." + return "System-managed internal job." + def _list_jobs(self) -> str: jobs = self._cron.list_jobs() if not jobs: @@ -227,6 +233,9 @@ class CronTool(Tool): for j in jobs: timing = self._format_timing(j.schedule) parts = [f"- {j.name} (id: {j.id}, {timing})"] + if j.payload.kind == "system_event": + parts.append(f" Purpose: {self._system_job_purpose(j)}") + parts.append(" Protected: visible for inspection, but cannot be removed.") parts.extend(self._format_state(j.state, j.schedule)) lines.append("\n".join(parts)) return "Scheduled jobs:\n" + "\n".join(lines) @@ -234,6 +243,19 @@ class CronTool(Tool): def _remove_job(self, job_id: str | None) -> str: if not job_id: return "Error: job_id is required for remove" - if self._cron.remove_job(job_id): + result = self._cron.remove_job(job_id) + if result == "removed": return f"Removed job {job_id}" + if result == "protected": + job = self._cron.get_job(job_id) + if job and job.name == "dream": + return ( + "Cannot remove job `dream`.\n" + "This is a system-managed Dream memory consolidation job for long-term memory.\n" + "It remains visible so you can inspect it, but it cannot be removed." + ) + return ( + f"Cannot remove job `{job_id}`.\n" + "This is a protected system-managed cron job." + ) return f"Job {job_id} not found" diff --git a/nanobot/channels/telegram.py b/nanobot/channels/telegram.py index a6bd810f2..3ba84c6c6 100644 --- a/nanobot/channels/telegram.py +++ b/nanobot/channels/telegram.py @@ -19,6 +19,7 @@ from telegram.request import HTTPXRequest from nanobot.bus.events import OutboundMessage from nanobot.bus.queue import MessageBus from nanobot.channels.base import BaseChannel +from nanobot.command.builtin import build_help_text from nanobot.config.paths import get_media_dir from nanobot.config.schema import Base from nanobot.security.network import validate_url_target @@ -196,9 +197,12 @@ class TelegramChannel(BaseChannel): BotCommand("start", "Start the bot"), BotCommand("new", "Start a new conversation"), BotCommand("stop", "Stop the current task"), - BotCommand("help", "Show available commands"), BotCommand("restart", "Restart the bot"), BotCommand("status", "Show bot status"), + BotCommand("dream", "Run Dream memory consolidation now"), + BotCommand("dream-log", "Show the latest Dream memory change"), + BotCommand("dream-restore", "Restore Dream memory to an earlier version"), + BotCommand("help", "Show available commands"), ] @classmethod @@ -277,7 +281,18 @@ class TelegramChannel(BaseChannel): # Add command handlers (using Regex to support @username suffixes before bot initialization) self._app.add_handler(MessageHandler(filters.Regex(r"^/start(?:@\w+)?$"), self._on_start)) - self._app.add_handler(MessageHandler(filters.Regex(r"^/(new|stop|restart|status)(?:@\w+)?$"), self._forward_command)) + self._app.add_handler( + MessageHandler( + filters.Regex(r"^/(new|stop|restart|status|dream)(?:@\w+)?(?:\s+.*)?$"), + self._forward_command, + ) + ) + self._app.add_handler( + MessageHandler( + filters.Regex(r"^/(dream-log|dream-restore)(?:@\w+)?(?:\s+.*)?$"), + self._forward_command, + ) + ) self._app.add_handler(MessageHandler(filters.Regex(r"^/help(?:@\w+)?$"), self._on_help)) # Add message handler for text, photos, voice, documents @@ -599,14 +614,7 @@ class TelegramChannel(BaseChannel): """Handle /help command, bypassing ACL so all users can access it.""" if not update.message: return - await update.message.reply_text( - "🐈 nanobot commands:\n" - "/new — Start a new conversation\n" - "/stop — Stop the current task\n" - "/restart — Restart the bot\n" - "/status — Show bot status\n" - "/help — Show available commands" - ) + await update.message.reply_text(build_help_text()) @staticmethod def _sender_id(user) -> str: diff --git a/nanobot/cli/commands.py b/nanobot/cli/commands.py index dcbf66746..88f13215c 100644 --- a/nanobot/cli/commands.py +++ b/nanobot/cli/commands.py @@ -22,6 +22,7 @@ if sys.platform == "win32": pass import typer +from loguru import logger from prompt_toolkit import PromptSession, print_formatted_text from prompt_toolkit.application import run_in_terminal from prompt_toolkit.formatted_text import ANSI, HTML @@ -652,6 +653,15 @@ def gateway( # Set cron callback (needs agent) async def on_cron_job(job: CronJob) -> str | None: """Execute a cron job through the agent.""" + # Dream is an internal job — run directly, not through the agent loop. + if job.name == "dream": + try: + await agent.dream.run() + logger.info("Dream cron job completed") + except Exception: + logger.exception("Dream cron job failed") + return None + from nanobot.agent.tools.cron import CronTool from nanobot.agent.tools.message import MessageTool from nanobot.utils.evaluator import evaluate_response @@ -771,6 +781,21 @@ def gateway( console.print(f"[green]✓[/green] Heartbeat: every {hb_cfg.interval_s}s") + # Register Dream system job (always-on, idempotent on restart) + dream_cfg = config.agents.defaults.dream + if dream_cfg.model_override: + agent.dream.model = dream_cfg.model_override + agent.dream.max_batch_size = dream_cfg.max_batch_size + agent.dream.max_iterations = dream_cfg.max_iterations + from nanobot.cron.types import CronJob, CronPayload + cron.register_system_job(CronJob( + id="dream", + name="dream", + schedule=dream_cfg.build_schedule(config.agents.defaults.timezone), + payload=CronPayload(kind="system_event"), + )) + console.print(f"[green]✓[/green] Dream: {dream_cfg.describe_schedule()}") + async def run(): try: await cron.start() diff --git a/nanobot/command/builtin.py b/nanobot/command/builtin.py index fa8dd693b..a5629f66e 100644 --- a/nanobot/command/builtin.py +++ b/nanobot/command/builtin.py @@ -55,7 +55,7 @@ async def cmd_status(ctx: CommandContext) -> OutboundMessage: session = ctx.session or loop.sessions.get_or_create(ctx.key) ctx_est = 0 try: - ctx_est, _ = loop.memory_consolidator.estimate_session_prompt_tokens(session) + ctx_est, _ = loop.consolidator.estimate_session_prompt_tokens(session) except Exception: pass if ctx_est <= 0: @@ -83,7 +83,7 @@ async def cmd_new(ctx: CommandContext) -> OutboundMessage: loop.sessions.save(session) loop.sessions.invalidate(session.key) if snapshot: - loop._schedule_background(loop.memory_consolidator.archive_messages(snapshot)) + loop._schedule_background(loop.consolidator.archive(snapshot)) return OutboundMessage( channel=ctx.msg.channel, chat_id=ctx.msg.chat_id, content="New session started.", @@ -91,6 +91,187 @@ async def cmd_new(ctx: CommandContext) -> OutboundMessage: ) +async def cmd_dream(ctx: CommandContext) -> OutboundMessage: + """Manually trigger a Dream consolidation run.""" + loop = ctx.loop + try: + did_work = await loop.dream.run() + content = "Dream completed." if did_work else "Dream: nothing to process." + except Exception as e: + content = f"Dream failed: {e}" + return OutboundMessage( + channel=ctx.msg.channel, chat_id=ctx.msg.chat_id, content=content, + ) + + +def _extract_changed_files(diff: str) -> list[str]: + """Extract changed file paths from a unified diff.""" + files: list[str] = [] + seen: set[str] = set() + for line in diff.splitlines(): + if not line.startswith("diff --git "): + continue + parts = line.split() + if len(parts) < 4: + continue + path = parts[3] + if path.startswith("b/"): + path = path[2:] + if path in seen: + continue + seen.add(path) + files.append(path) + return files + + +def _format_changed_files(diff: str) -> str: + files = _extract_changed_files(diff) + if not files: + return "No tracked memory files changed." + return ", ".join(f"`{path}`" for path in files) + + +def _format_dream_log_content(commit, diff: str, *, requested_sha: str | None = None) -> str: + files_line = _format_changed_files(diff) + lines = [ + "## Dream Update", + "", + "Here is the selected Dream memory change." if requested_sha else "Here is the latest Dream memory change.", + "", + f"- Commit: `{commit.sha}`", + f"- Time: {commit.timestamp}", + f"- Changed files: {files_line}", + ] + if diff: + lines.extend([ + "", + f"Use `/dream-restore {commit.sha}` to undo this change.", + "", + "```diff", + diff.rstrip(), + "```", + ]) + else: + lines.extend([ + "", + "Dream recorded this version, but there is no file diff to display.", + ]) + return "\n".join(lines) + + +def _format_dream_restore_list(commits: list) -> str: + lines = [ + "## Dream Restore", + "", + "Choose a Dream memory version to restore. Latest first:", + "", + ] + for c in commits: + lines.append(f"- `{c.sha}` {c.timestamp} - {c.message.splitlines()[0]}") + lines.extend([ + "", + "Preview a version with `/dream-log ` before restoring it.", + "Restore a version with `/dream-restore `.", + ]) + return "\n".join(lines) + + +async def cmd_dream_log(ctx: CommandContext) -> OutboundMessage: + """Show what the last Dream changed. + + Default: diff of the latest commit (HEAD~1 vs HEAD). + With /dream-log : diff of that specific commit. + """ + store = ctx.loop.consolidator.store + git = store.git + + if not git.is_initialized(): + if store.get_last_dream_cursor() == 0: + msg = "Dream has not run yet. Run `/dream`, or wait for the next scheduled Dream cycle." + else: + msg = "Dream history is not available because memory versioning is not initialized." + return OutboundMessage( + channel=ctx.msg.channel, chat_id=ctx.msg.chat_id, + content=msg, metadata={"render_as": "text"}, + ) + + args = ctx.args.strip() + + if args: + # Show diff of a specific commit + sha = args.split()[0] + result = git.show_commit_diff(sha) + if not result: + content = ( + f"Couldn't find Dream change `{sha}`.\n\n" + "Use `/dream-restore` to list recent versions, " + "or `/dream-log` to inspect the latest one." + ) + else: + commit, diff = result + content = _format_dream_log_content(commit, diff, requested_sha=sha) + else: + # Default: show the latest commit's diff + commits = git.log(max_entries=1) + result = git.show_commit_diff(commits[0].sha) if commits else None + if result: + commit, diff = result + content = _format_dream_log_content(commit, diff) + else: + content = "Dream memory has no saved versions yet." + + return OutboundMessage( + channel=ctx.msg.channel, chat_id=ctx.msg.chat_id, + content=content, metadata={"render_as": "text"}, + ) + + +async def cmd_dream_restore(ctx: CommandContext) -> OutboundMessage: + """Restore memory files from a previous dream commit. + + Usage: + /dream-restore — list recent commits + /dream-restore — revert a specific commit + """ + store = ctx.loop.consolidator.store + git = store.git + if not git.is_initialized(): + return OutboundMessage( + channel=ctx.msg.channel, chat_id=ctx.msg.chat_id, + content="Dream history is not available because memory versioning is not initialized.", + ) + + args = ctx.args.strip() + if not args: + # Show recent commits for the user to pick + commits = git.log(max_entries=10) + if not commits: + content = "Dream memory has no saved versions to restore yet." + else: + content = _format_dream_restore_list(commits) + else: + sha = args.split()[0] + result = git.show_commit_diff(sha) + changed_files = _format_changed_files(result[1]) if result else "the tracked memory files" + new_sha = git.revert(sha) + if new_sha: + content = ( + f"Restored Dream memory to the state before `{sha}`.\n\n" + f"- New safety commit: `{new_sha}`\n" + f"- Restored files: {changed_files}\n\n" + f"Use `/dream-log {new_sha}` to inspect the restore diff." + ) + else: + content = ( + f"Couldn't restore Dream change `{sha}`.\n\n" + "It may not exist, or it may be the first saved version with no earlier state to restore." + ) + return OutboundMessage( + channel=ctx.msg.channel, chat_id=ctx.msg.chat_id, + content=content, metadata={"render_as": "text"}, + ) + + async def cmd_help(ctx: CommandContext) -> OutboundMessage: """Return available slash commands.""" return OutboundMessage( @@ -109,6 +290,9 @@ def build_help_text() -> str: "/stop — Stop the current task", "/restart — Restart the bot", "/status — Show bot status", + "/dream — Manually trigger Dream consolidation", + "/dream-log — Show what the last Dream changed", + "/dream-restore — Revert memory to a previous state", "/help — Show available commands", ] return "\n".join(lines) @@ -121,4 +305,9 @@ def register_builtin_commands(router: CommandRouter) -> None: router.priority("/status", cmd_status) router.exact("/new", cmd_new) router.exact("/status", cmd_status) + router.exact("/dream", cmd_dream) + router.exact("/dream-log", cmd_dream_log) + router.prefix("/dream-log ", cmd_dream_log) + router.exact("/dream-restore", cmd_dream_restore) + router.prefix("/dream-restore ", cmd_dream_restore) router.exact("/help", cmd_help) diff --git a/nanobot/config/schema.py b/nanobot/config/schema.py index cc377cfe0..0999bd99e 100644 --- a/nanobot/config/schema.py +++ b/nanobot/config/schema.py @@ -3,10 +3,12 @@ from pathlib import Path from typing import Literal -from pydantic import BaseModel, ConfigDict, Field +from pydantic import AliasChoices, BaseModel, ConfigDict, Field from pydantic.alias_generators import to_camel from pydantic_settings import BaseSettings +from nanobot.cron.types import CronSchedule + class Base(BaseModel): """Base model that accepts both camelCase and snake_case keys.""" @@ -28,6 +30,34 @@ class ChannelsConfig(Base): send_max_retries: int = Field(default=3, ge=0, le=10) # Max delivery attempts (initial send included) +class DreamConfig(Base): + """Dream memory consolidation configuration.""" + + _HOUR_MS = 3_600_000 + + interval_h: int = Field(default=2, ge=1) # Every 2 hours by default + cron: str | None = Field(default=None, exclude=True) # Legacy compatibility override + model_override: str | None = Field( + default=None, + validation_alias=AliasChoices("modelOverride", "model", "model_override"), + ) # Optional Dream-specific model override + max_batch_size: int = Field(default=20, ge=1) # Max history entries per run + max_iterations: int = Field(default=10, ge=1) # Max tool calls per Phase 2 + + def build_schedule(self, timezone: str) -> CronSchedule: + """Build the runtime schedule, preferring the legacy cron override if present.""" + if self.cron: + return CronSchedule(kind="cron", expr=self.cron, tz=timezone) + return CronSchedule(kind="every", every_ms=self.interval_h * self._HOUR_MS) + + def describe_schedule(self) -> str: + """Return a human-readable summary for logs and startup output.""" + if self.cron: + return f"cron {self.cron} (legacy)" + hours = self.interval_h + return f"every {hours}h" + + class AgentDefaults(Base): """Default agent configuration.""" @@ -45,6 +75,7 @@ class AgentDefaults(Base): provider_retry_mode: Literal["standard", "persistent"] = "standard" reasoning_effort: str | None = None # low / medium / high - enables LLM thinking mode timezone: str = "UTC" # IANA timezone, e.g. "Asia/Shanghai", "America/New_York" + dream: DreamConfig = Field(default_factory=DreamConfig) class AgentsConfig(Base): diff --git a/nanobot/cron/service.py b/nanobot/cron/service.py index c956b897f..d60846640 100644 --- a/nanobot/cron/service.py +++ b/nanobot/cron/service.py @@ -6,7 +6,7 @@ import time import uuid from datetime import datetime from pathlib import Path -from typing import Any, Callable, Coroutine +from typing import Any, Callable, Coroutine, Literal from loguru import logger @@ -351,9 +351,30 @@ class CronService: logger.info("Cron: added job '{}' ({})", name, job.id) return job - def remove_job(self, job_id: str) -> bool: - """Remove a job by ID.""" + def register_system_job(self, job: CronJob) -> CronJob: + """Register an internal system job (idempotent on restart).""" store = self._load_store() + now = _now_ms() + job.state = CronJobState(next_run_at_ms=_compute_next_run(job.schedule, now)) + job.created_at_ms = now + job.updated_at_ms = now + store.jobs = [j for j in store.jobs if j.id != job.id] + store.jobs.append(job) + self._save_store() + self._arm_timer() + logger.info("Cron: registered system job '{}' ({})", job.name, job.id) + return job + + def remove_job(self, job_id: str) -> Literal["removed", "protected", "not_found"]: + """Remove a job by ID, unless it is a protected system job.""" + store = self._load_store() + job = next((j for j in store.jobs if j.id == job_id), None) + if job is None: + return "not_found" + if job.payload.kind == "system_event": + logger.info("Cron: refused to remove protected system job {}", job_id) + return "protected" + before = len(store.jobs) store.jobs = [j for j in store.jobs if j.id != job_id] removed = len(store.jobs) < before @@ -362,8 +383,9 @@ class CronService: self._save_store() self._arm_timer() logger.info("Cron: removed job {}", job_id) + return "removed" - return removed + return "not_found" def enable_job(self, job_id: str, enabled: bool = True) -> CronJob | None: """Enable or disable a job.""" diff --git a/nanobot/skills/memory/SKILL.md b/nanobot/skills/memory/SKILL.md index 3f0a8fc2b..b47f2635c 100644 --- a/nanobot/skills/memory/SKILL.md +++ b/nanobot/skills/memory/SKILL.md @@ -1,6 +1,6 @@ --- name: memory -description: Two-layer memory system with grep-based recall. +description: Two-layer memory system with Dream-managed knowledge files. always: true --- @@ -8,30 +8,22 @@ always: true ## Structure -- `memory/MEMORY.md` — Long-term facts (preferences, project context, relationships). Always loaded into your context. -- `memory/HISTORY.md` — Append-only event log. NOT loaded into context. Search it with grep-style tools or in-memory filters. Each entry starts with [YYYY-MM-DD HH:MM]. +- `SOUL.md` — Bot personality and communication style. **Managed by Dream.** Do NOT edit. +- `USER.md` — User profile and preferences. **Managed by Dream.** Do NOT edit. +- `memory/MEMORY.md` — Long-term facts (project context, important events). **Managed by Dream.** Do NOT edit. +- `memory/history.jsonl` — append-only JSONL, not loaded into context. search with `jq`-style tools. ## Search Past Events -Choose the search method based on file size: +`memory/history.jsonl` is JSONL format — each line is a JSON object with `cursor`, `timestamp`, `content`. -- Small `memory/HISTORY.md`: use `read_file`, then search in-memory -- Large or long-lived `memory/HISTORY.md`: use the `exec` tool for targeted search +Examples (replace `keyword`): +- **Python (cross-platform):** `python -c "import json; [print(json.loads(l).get('content','')) for l in open('memory/history.jsonl','r',encoding='utf-8') if l.strip() and 'keyword' in l.lower()][-20:]"` +- **jq:** `cat memory/history.jsonl | jq -r 'select(.content | test("keyword"; "i")) | .content' | tail -20` +- **grep:** `grep -i "keyword" memory/history.jsonl` -Examples: -- **Linux/macOS:** `grep -i "keyword" memory/HISTORY.md` -- **Windows:** `findstr /i "keyword" memory\HISTORY.md` -- **Cross-platform Python:** `python -c "from pathlib import Path; text = Path('memory/HISTORY.md').read_text(encoding='utf-8'); print('\n'.join([l for l in text.splitlines() if 'keyword' in l.lower()][-20:]))"` +## Important -Prefer targeted command-line search for large history files. - -## When to Update MEMORY.md - -Write important facts immediately using `edit_file` or `write_file`: -- User preferences ("I prefer dark mode") -- Project context ("The API uses OAuth2") -- Relationships ("Alice is the project lead") - -## Auto-consolidation - -Old conversations are automatically summarized and appended to HISTORY.md when the session grows large. Long-term facts are extracted to MEMORY.md. You don't need to manage this. +- **Do NOT edit SOUL.md, USER.md, or MEMORY.md.** They are automatically managed by Dream. +- If you notice outdated information, it will be corrected when Dream runs next. +- Users can view Dream's activity with the `/dream-log` command. diff --git a/nanobot/utils/gitstore.py b/nanobot/utils/gitstore.py new file mode 100644 index 000000000..c2f7d2372 --- /dev/null +++ b/nanobot/utils/gitstore.py @@ -0,0 +1,307 @@ +"""Git-backed version control for memory files, using dulwich.""" + +from __future__ import annotations + +import io +import time +from dataclasses import dataclass +from pathlib import Path + +from loguru import logger + + +@dataclass +class CommitInfo: + sha: str # Short SHA (8 chars) + message: str + timestamp: str # Formatted datetime + + def format(self, diff: str = "") -> str: + """Format this commit for display, optionally with a diff.""" + header = f"## {self.message.splitlines()[0]}\n`{self.sha}` — {self.timestamp}\n" + if diff: + return f"{header}\n```diff\n{diff}\n```" + return f"{header}\n(no file changes)" + + +class GitStore: + """Git-backed version control for memory files.""" + + def __init__(self, workspace: Path, tracked_files: list[str]): + self._workspace = workspace + self._tracked_files = tracked_files + + def is_initialized(self) -> bool: + """Check if the git repo has been initialized.""" + return (self._workspace / ".git").is_dir() + + # -- init ------------------------------------------------------------------ + + def init(self) -> bool: + """Initialize a git repo if not already initialized. + + Creates .gitignore and makes an initial commit. + Returns True if a new repo was created, False if already exists. + """ + if self.is_initialized(): + return False + + try: + from dulwich import porcelain + + porcelain.init(str(self._workspace)) + + # Write .gitignore + gitignore = self._workspace / ".gitignore" + gitignore.write_text(self._build_gitignore(), encoding="utf-8") + + # Ensure tracked files exist (touch them if missing) so the initial + # commit has something to track. + for rel in self._tracked_files: + p = self._workspace / rel + p.parent.mkdir(parents=True, exist_ok=True) + if not p.exists(): + p.write_text("", encoding="utf-8") + + # Initial commit + porcelain.add(str(self._workspace), paths=[".gitignore"] + self._tracked_files) + porcelain.commit( + str(self._workspace), + message=b"init: nanobot memory store", + author=b"nanobot ", + committer=b"nanobot ", + ) + logger.info("Git store initialized at {}", self._workspace) + return True + except Exception: + logger.warning("Git store init failed for {}", self._workspace) + return False + + # -- daily operations ------------------------------------------------------ + + def auto_commit(self, message: str) -> str | None: + """Stage tracked memory files and commit if there are changes. + + Returns the short commit SHA, or None if nothing to commit. + """ + if not self.is_initialized(): + return None + + try: + from dulwich import porcelain + + # .gitignore excludes everything except tracked files, + # so any staged/unstaged change must be in our files. + st = porcelain.status(str(self._workspace)) + if not st.unstaged and not any(st.staged.values()): + return None + + msg_bytes = message.encode("utf-8") if isinstance(message, str) else message + porcelain.add(str(self._workspace), paths=self._tracked_files) + sha_bytes = porcelain.commit( + str(self._workspace), + message=msg_bytes, + author=b"nanobot ", + committer=b"nanobot ", + ) + if sha_bytes is None: + return None + sha = sha_bytes.hex()[:8] + logger.debug("Git auto-commit: {} ({})", sha, message) + return sha + except Exception: + logger.warning("Git auto-commit failed: {}", message) + return None + + # -- internal helpers ------------------------------------------------------ + + def _resolve_sha(self, short_sha: str) -> bytes | None: + """Resolve a short SHA prefix to the full SHA bytes.""" + try: + from dulwich.repo import Repo + + with Repo(str(self._workspace)) as repo: + try: + sha = repo.refs[b"HEAD"] + except KeyError: + return None + + while sha: + if sha.hex().startswith(short_sha): + return sha + commit = repo[sha] + if commit.type_name != b"commit": + break + sha = commit.parents[0] if commit.parents else None + return None + except Exception: + return None + + def _build_gitignore(self) -> str: + """Generate .gitignore content from tracked files.""" + dirs: set[str] = set() + for f in self._tracked_files: + parent = str(Path(f).parent) + if parent != ".": + dirs.add(parent) + lines = ["/*"] + for d in sorted(dirs): + lines.append(f"!{d}/") + for f in self._tracked_files: + lines.append(f"!{f}") + lines.append("!.gitignore") + return "\n".join(lines) + "\n" + + # -- query ----------------------------------------------------------------- + + def log(self, max_entries: int = 20) -> list[CommitInfo]: + """Return simplified commit log.""" + if not self.is_initialized(): + return [] + + try: + from dulwich.repo import Repo + + entries: list[CommitInfo] = [] + with Repo(str(self._workspace)) as repo: + try: + head = repo.refs[b"HEAD"] + except KeyError: + return [] + + sha = head + while sha and len(entries) < max_entries: + commit = repo[sha] + if commit.type_name != b"commit": + break + ts = time.strftime( + "%Y-%m-%d %H:%M", + time.localtime(commit.commit_time), + ) + msg = commit.message.decode("utf-8", errors="replace").strip() + entries.append(CommitInfo( + sha=sha.hex()[:8], + message=msg, + timestamp=ts, + )) + sha = commit.parents[0] if commit.parents else None + + return entries + except Exception: + logger.warning("Git log failed") + return [] + + def diff_commits(self, sha1: str, sha2: str) -> str: + """Show diff between two commits.""" + if not self.is_initialized(): + return "" + + try: + from dulwich import porcelain + + full1 = self._resolve_sha(sha1) + full2 = self._resolve_sha(sha2) + if not full1 or not full2: + return "" + + out = io.BytesIO() + porcelain.diff( + str(self._workspace), + commit=full1, + commit2=full2, + outstream=out, + ) + return out.getvalue().decode("utf-8", errors="replace") + except Exception: + logger.warning("Git diff_commits failed") + return "" + + def find_commit(self, short_sha: str, max_entries: int = 20) -> CommitInfo | None: + """Find a commit by short SHA prefix match.""" + for c in self.log(max_entries=max_entries): + if c.sha.startswith(short_sha): + return c + return None + + def show_commit_diff(self, short_sha: str, max_entries: int = 20) -> tuple[CommitInfo, str] | None: + """Find a commit and return it with its diff vs the parent.""" + commits = self.log(max_entries=max_entries) + for i, c in enumerate(commits): + if c.sha.startswith(short_sha): + if i + 1 < len(commits): + diff = self.diff_commits(commits[i + 1].sha, c.sha) + else: + diff = "" + return c, diff + return None + + # -- restore --------------------------------------------------------------- + + def revert(self, commit: str) -> str | None: + """Revert (undo) the changes introduced by the given commit. + + Restores all tracked memory files to the state at the commit's parent, + then creates a new commit recording the revert. + + Returns the new commit SHA, or None on failure. + """ + if not self.is_initialized(): + return None + + try: + from dulwich.repo import Repo + + full_sha = self._resolve_sha(commit) + if not full_sha: + logger.warning("Git revert: SHA not found: {}", commit) + return None + + with Repo(str(self._workspace)) as repo: + commit_obj = repo[full_sha] + if commit_obj.type_name != b"commit": + return None + + if not commit_obj.parents: + logger.warning("Git revert: cannot revert root commit {}", commit) + return None + + # Use the parent's tree — this undoes the commit's changes + parent_obj = repo[commit_obj.parents[0]] + tree = repo[parent_obj.tree] + + restored: list[str] = [] + for filepath in self._tracked_files: + content = self._read_blob_from_tree(repo, tree, filepath) + if content is not None: + dest = self._workspace / filepath + dest.write_text(content, encoding="utf-8") + restored.append(filepath) + + if not restored: + return None + + # Commit the restored state + msg = f"revert: undo {commit}" + return self.auto_commit(msg) + except Exception: + logger.warning("Git revert failed for {}", commit) + return None + + @staticmethod + def _read_blob_from_tree(repo, tree, filepath: str) -> str | None: + """Read a blob's content from a tree object by walking path parts.""" + parts = Path(filepath).parts + current = tree + for part in parts: + try: + entry = current[part.encode()] + except KeyError: + return None + obj = repo[entry[1]] + if obj.type_name == b"blob": + return obj.data.decode("utf-8", errors="replace") + if obj.type_name == b"tree": + current = obj + else: + return None + return None diff --git a/nanobot/utils/helpers.py b/nanobot/utils/helpers.py index 9e0a69d5e..93293c9e0 100644 --- a/nanobot/utils/helpers.py +++ b/nanobot/utils/helpers.py @@ -447,11 +447,22 @@ def sync_workspace_templates(workspace: Path, silent: bool = False) -> list[str] if item.name.endswith(".md") and not item.name.startswith("."): _write(item, workspace / item.name) _write(tpl / "memory" / "MEMORY.md", workspace / "memory" / "MEMORY.md") - _write(None, workspace / "memory" / "HISTORY.md") + _write(None, workspace / "memory" / "history.jsonl") (workspace / "skills").mkdir(exist_ok=True) if added and not silent: from rich.console import Console for name in added: Console().print(f" [dim]Created {name}[/dim]") + + # Initialize git for memory version control + try: + from nanobot.utils.gitstore import GitStore + gs = GitStore(workspace, tracked_files=[ + "SOUL.md", "USER.md", "memory/MEMORY.md", + ]) + gs.init() + except Exception: + logger.warning("Failed to initialize git store for {}", workspace) + return added diff --git a/pyproject.toml b/pyproject.toml index 51d494668..a00cf6bc6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -48,6 +48,7 @@ dependencies = [ "chardet>=3.0.2,<6.0.0", "openai>=2.8.0", "tiktoken>=0.12.0,<1.0.0", + "dulwich>=0.22.0,<1.0.0", ] [project.optional-dependencies] diff --git a/tests/agent/test_consolidate_offset.py b/tests/agent/test_consolidate_offset.py index 4f2e8f1c2..f6232c348 100644 --- a/tests/agent/test_consolidate_offset.py +++ b/tests/agent/test_consolidate_offset.py @@ -506,7 +506,7 @@ class TestNewCommandArchival: @pytest.mark.asyncio async def test_new_clears_session_immediately_even_if_archive_fails(self, tmp_path: Path) -> None: - """/new clears session immediately; archive_messages retries until raw dump.""" + """/new clears session immediately; archive is fire-and-forget.""" from nanobot.bus.events import InboundMessage loop = self._make_loop(tmp_path) @@ -518,12 +518,12 @@ class TestNewCommandArchival: call_count = 0 - async def _failing_consolidate(_messages) -> bool: + async def _failing_summarize(_messages) -> bool: nonlocal call_count call_count += 1 return False - loop.memory_consolidator.consolidate_messages = _failing_consolidate # type: ignore[method-assign] + loop.consolidator.archive = _failing_summarize # type: ignore[method-assign] new_msg = InboundMessage(channel="cli", sender_id="user", chat_id="test", content="/new") response = await loop._process_message(new_msg) @@ -535,7 +535,7 @@ class TestNewCommandArchival: assert len(session_after.messages) == 0 await loop.close_mcp() - assert call_count == 3 # retried up to raw-archive threshold + assert call_count == 1 @pytest.mark.asyncio async def test_new_archives_only_unconsolidated_messages(self, tmp_path: Path) -> None: @@ -551,12 +551,12 @@ class TestNewCommandArchival: archived_count = -1 - async def _fake_consolidate(messages) -> bool: + async def _fake_summarize(messages) -> bool: nonlocal archived_count archived_count = len(messages) return True - loop.memory_consolidator.consolidate_messages = _fake_consolidate # type: ignore[method-assign] + loop.consolidator.archive = _fake_summarize # type: ignore[method-assign] new_msg = InboundMessage(channel="cli", sender_id="user", chat_id="test", content="/new") response = await loop._process_message(new_msg) @@ -578,10 +578,10 @@ class TestNewCommandArchival: session.add_message("assistant", f"resp{i}") loop.sessions.save(session) - async def _ok_consolidate(_messages) -> bool: + async def _ok_summarize(_messages) -> bool: return True - loop.memory_consolidator.consolidate_messages = _ok_consolidate # type: ignore[method-assign] + loop.consolidator.archive = _ok_summarize # type: ignore[method-assign] new_msg = InboundMessage(channel="cli", sender_id="user", chat_id="test", content="/new") response = await loop._process_message(new_msg) @@ -604,12 +604,12 @@ class TestNewCommandArchival: archived = asyncio.Event() - async def _slow_consolidate(_messages) -> bool: + async def _slow_summarize(_messages) -> bool: await asyncio.sleep(0.1) archived.set() return True - loop.memory_consolidator.consolidate_messages = _slow_consolidate # type: ignore[method-assign] + loop.consolidator.archive = _slow_summarize # type: ignore[method-assign] new_msg = InboundMessage(channel="cli", sender_id="user", chat_id="test", content="/new") await loop._process_message(new_msg) diff --git a/tests/agent/test_consolidator.py b/tests/agent/test_consolidator.py new file mode 100644 index 000000000..72968b0e1 --- /dev/null +++ b/tests/agent/test_consolidator.py @@ -0,0 +1,78 @@ +"""Tests for the lightweight Consolidator — append-only to HISTORY.md.""" + +import pytest +import asyncio +from unittest.mock import AsyncMock, MagicMock, patch + +from nanobot.agent.memory import Consolidator, MemoryStore + + +@pytest.fixture +def store(tmp_path): + return MemoryStore(tmp_path) + + +@pytest.fixture +def mock_provider(): + p = MagicMock() + p.chat_with_retry = AsyncMock() + return p + + +@pytest.fixture +def consolidator(store, mock_provider): + sessions = MagicMock() + sessions.save = MagicMock() + return Consolidator( + store=store, + provider=mock_provider, + model="test-model", + sessions=sessions, + context_window_tokens=1000, + build_messages=MagicMock(return_value=[]), + get_tool_definitions=MagicMock(return_value=[]), + max_completion_tokens=100, + ) + + +class TestConsolidatorSummarize: + async def test_summarize_appends_to_history(self, consolidator, mock_provider, store): + """Consolidator should call LLM to summarize, then append to HISTORY.md.""" + mock_provider.chat_with_retry.return_value = MagicMock( + content="User fixed a bug in the auth module." + ) + messages = [ + {"role": "user", "content": "fix the auth bug"}, + {"role": "assistant", "content": "Done, fixed the race condition."}, + ] + result = await consolidator.archive(messages) + assert result is True + entries = store.read_unprocessed_history(since_cursor=0) + assert len(entries) == 1 + + async def test_summarize_raw_dumps_on_llm_failure(self, consolidator, mock_provider, store): + """On LLM failure, raw-dump messages to HISTORY.md.""" + mock_provider.chat_with_retry.side_effect = Exception("API error") + messages = [{"role": "user", "content": "hello"}] + result = await consolidator.archive(messages) + assert result is True # always succeeds + entries = store.read_unprocessed_history(since_cursor=0) + assert len(entries) == 1 + assert "[RAW]" in entries[0]["content"] + + async def test_summarize_skips_empty_messages(self, consolidator): + result = await consolidator.archive([]) + assert result is False + + +class TestConsolidatorTokenBudget: + async def test_prompt_below_threshold_does_not_consolidate(self, consolidator): + """No consolidation when tokens are within budget.""" + session = MagicMock() + session.last_consolidated = 0 + session.messages = [{"role": "user", "content": "hi"}] + session.key = "test:key" + consolidator.estimate_session_prompt_tokens = MagicMock(return_value=(100, "tiktoken")) + consolidator.archive = AsyncMock(return_value=True) + await consolidator.maybe_consolidate_by_tokens(session) + consolidator.archive.assert_not_called() diff --git a/tests/agent/test_dream.py b/tests/agent/test_dream.py new file mode 100644 index 000000000..7898ea267 --- /dev/null +++ b/tests/agent/test_dream.py @@ -0,0 +1,97 @@ +"""Tests for the Dream class — two-phase memory consolidation via AgentRunner.""" + +import pytest + +from unittest.mock import AsyncMock, MagicMock + +from nanobot.agent.memory import Dream, MemoryStore +from nanobot.agent.runner import AgentRunResult + + +@pytest.fixture +def store(tmp_path): + s = MemoryStore(tmp_path) + s.write_soul("# Soul\n- Helpful") + s.write_user("# User\n- Developer") + s.write_memory("# Memory\n- Project X active") + return s + + +@pytest.fixture +def mock_provider(): + p = MagicMock() + p.chat_with_retry = AsyncMock() + return p + + +@pytest.fixture +def mock_runner(): + return MagicMock() + + +@pytest.fixture +def dream(store, mock_provider, mock_runner): + d = Dream(store=store, provider=mock_provider, model="test-model", max_batch_size=5) + d._runner = mock_runner + return d + + +def _make_run_result( + stop_reason="completed", + final_content=None, + tool_events=None, + usage=None, +): + return AgentRunResult( + final_content=final_content or stop_reason, + stop_reason=stop_reason, + messages=[], + tools_used=[], + usage={}, + tool_events=tool_events or [], + ) + + +class TestDreamRun: + async def test_noop_when_no_unprocessed_history(self, dream, mock_provider, mock_runner, store): + """Dream should not call LLM when there's nothing to process.""" + result = await dream.run() + assert result is False + mock_provider.chat_with_retry.assert_not_called() + mock_runner.run.assert_not_called() + + async def test_calls_runner_for_unprocessed_entries(self, dream, mock_provider, mock_runner, store): + """Dream should call AgentRunner when there are unprocessed history entries.""" + store.append_history("User prefers dark mode") + mock_provider.chat_with_retry.return_value = MagicMock(content="New fact") + mock_runner.run = AsyncMock(return_value=_make_run_result( + tool_events=[{"name": "edit_file", "status": "ok", "detail": "memory/MEMORY.md"}], + )) + result = await dream.run() + assert result is True + mock_runner.run.assert_called_once() + spec = mock_runner.run.call_args[0][0] + assert spec.max_iterations == 10 + assert spec.fail_on_tool_error is True + + async def test_advances_dream_cursor(self, dream, mock_provider, mock_runner, store): + """Dream should advance the cursor after processing.""" + store.append_history("event 1") + store.append_history("event 2") + mock_provider.chat_with_retry.return_value = MagicMock(content="Nothing new") + mock_runner.run = AsyncMock(return_value=_make_run_result()) + await dream.run() + assert store.get_last_dream_cursor() == 2 + + async def test_compacts_processed_history(self, dream, mock_provider, mock_runner, store): + """Dream should compact history after processing.""" + store.append_history("event 1") + store.append_history("event 2") + store.append_history("event 3") + mock_provider.chat_with_retry.return_value = MagicMock(content="Nothing new") + mock_runner.run = AsyncMock(return_value=_make_run_result()) + await dream.run() + # After Dream, cursor is advanced and 3, compact keeps last max_history_entries + entries = store.read_unprocessed_history(since_cursor=0) + assert all(e["cursor"] > 0 for e in entries) + diff --git a/tests/agent/test_git_store.py b/tests/agent/test_git_store.py new file mode 100644 index 000000000..07cfa7919 --- /dev/null +++ b/tests/agent/test_git_store.py @@ -0,0 +1,234 @@ +"""Tests for GitStore — git-backed version control for memory files.""" + +import pytest +from pathlib import Path + +from nanobot.utils.gitstore import GitStore, CommitInfo + + +TRACKED = ["SOUL.md", "USER.md", "memory/MEMORY.md"] + + +@pytest.fixture +def git(tmp_path): + """Uninitialized GitStore.""" + return GitStore(tmp_path, tracked_files=TRACKED) + + +@pytest.fixture +def git_ready(git): + """Initialized GitStore with one initial commit.""" + git.init() + return git + + +class TestInit: + def test_not_initialized_by_default(self, git, tmp_path): + assert not git.is_initialized() + assert not (tmp_path / ".git").is_dir() + + def test_init_creates_git_dir(self, git, tmp_path): + assert git.init() + assert (tmp_path / ".git").is_dir() + + def test_init_idempotent(self, git_ready): + assert not git_ready.init() + + def test_init_creates_gitignore(self, git_ready): + gi = git_ready._workspace / ".gitignore" + assert gi.exists() + content = gi.read_text(encoding="utf-8") + for f in TRACKED: + assert f"!{f}" in content + + def test_init_touches_tracked_files(self, git_ready): + for f in TRACKED: + assert (git_ready._workspace / f).exists() + + def test_init_makes_initial_commit(self, git_ready): + commits = git_ready.log() + assert len(commits) == 1 + assert "init" in commits[0].message + + +class TestBuildGitignore: + def test_subdirectory_dirs(self, git): + content = git._build_gitignore() + assert "!memory/\n" in content + for f in TRACKED: + assert f"!{f}\n" in content + assert content.startswith("/*\n") + + def test_root_level_files_no_dir_entries(self, tmp_path): + gs = GitStore(tmp_path, tracked_files=["a.md", "b.md"]) + content = gs._build_gitignore() + assert "!a.md\n" in content + assert "!b.md\n" in content + dir_lines = [l for l in content.split("\n") if l.startswith("!") and l.endswith("/")] + assert dir_lines == [] + + +class TestAutoCommit: + def test_returns_none_when_not_initialized(self, git): + assert git.auto_commit("test") is None + + def test_commits_file_change(self, git_ready): + (git_ready._workspace / "SOUL.md").write_text("updated", encoding="utf-8") + sha = git_ready.auto_commit("update soul") + assert sha is not None + assert len(sha) == 8 + + def test_returns_none_when_no_changes(self, git_ready): + assert git_ready.auto_commit("no change") is None + + def test_commit_appears_in_log(self, git_ready): + ws = git_ready._workspace + (ws / "SOUL.md").write_text("v2", encoding="utf-8") + sha = git_ready.auto_commit("update soul") + commits = git_ready.log() + assert len(commits) == 2 + assert commits[0].sha == sha + + def test_does_not_create_empty_commits(self, git_ready): + git_ready.auto_commit("nothing 1") + git_ready.auto_commit("nothing 2") + assert len(git_ready.log()) == 1 # only init commit + + +class TestLog: + def test_empty_when_not_initialized(self, git): + assert git.log() == [] + + def test_newest_first(self, git_ready): + ws = git_ready._workspace + for i in range(3): + (ws / "SOUL.md").write_text(f"v{i}", encoding="utf-8") + git_ready.auto_commit(f"commit {i}") + + commits = git_ready.log() + assert len(commits) == 4 # init + 3 + assert "commit 2" in commits[0].message + assert "init" in commits[-1].message + + def test_max_entries(self, git_ready): + ws = git_ready._workspace + for i in range(10): + (ws / "SOUL.md").write_text(f"v{i}", encoding="utf-8") + git_ready.auto_commit(f"c{i}") + assert len(git_ready.log(max_entries=3)) == 3 + + def test_commit_info_fields(self, git_ready): + c = git_ready.log()[0] + assert isinstance(c, CommitInfo) + assert len(c.sha) == 8 + assert c.timestamp + assert c.message + + +class TestDiffCommits: + def test_empty_when_not_initialized(self, git): + assert git.diff_commits("a", "b") == "" + + def test_diff_between_two_commits(self, git_ready): + ws = git_ready._workspace + (ws / "SOUL.md").write_text("original", encoding="utf-8") + git_ready.auto_commit("v1") + (ws / "SOUL.md").write_text("modified", encoding="utf-8") + git_ready.auto_commit("v2") + + commits = git_ready.log() + diff = git_ready.diff_commits(commits[1].sha, commits[0].sha) + assert "modified" in diff + + def test_invalid_sha_returns_empty(self, git_ready): + assert git_ready.diff_commits("deadbeef", "cafebabe") == "" + + +class TestFindCommit: + def test_finds_by_prefix(self, git_ready): + ws = git_ready._workspace + (ws / "SOUL.md").write_text("v2", encoding="utf-8") + sha = git_ready.auto_commit("v2") + found = git_ready.find_commit(sha[:4]) + assert found is not None + assert found.sha == sha + + def test_returns_none_for_unknown(self, git_ready): + assert git_ready.find_commit("deadbeef") is None + + +class TestShowCommitDiff: + def test_returns_commit_with_diff(self, git_ready): + ws = git_ready._workspace + (ws / "SOUL.md").write_text("content", encoding="utf-8") + sha = git_ready.auto_commit("add content") + result = git_ready.show_commit_diff(sha) + assert result is not None + commit, diff = result + assert commit.sha == sha + assert "content" in diff + + def test_first_commit_has_empty_diff(self, git_ready): + init_sha = git_ready.log()[-1].sha + result = git_ready.show_commit_diff(init_sha) + assert result is not None + _, diff = result + assert diff == "" + + def test_returns_none_for_unknown(self, git_ready): + assert git_ready.show_commit_diff("deadbeef") is None + + +class TestCommitInfoFormat: + def test_format_with_diff(self): + from nanobot.utils.gitstore import CommitInfo + c = CommitInfo(sha="abcd1234", message="test commit\nsecond line", timestamp="2026-04-02 12:00") + result = c.format(diff="some diff") + assert "test commit" in result + assert "`abcd1234`" in result + assert "some diff" in result + + def test_format_without_diff(self): + from nanobot.utils.gitstore import CommitInfo + c = CommitInfo(sha="abcd1234", message="test", timestamp="2026-04-02 12:00") + result = c.format() + assert "(no file changes)" in result + + +class TestRevert: + def test_returns_none_when_not_initialized(self, git): + assert git.revert("abc") is None + + def test_undoes_commit_changes(self, git_ready): + """revert(sha) should undo the given commit by restoring to its parent.""" + ws = git_ready._workspace + (ws / "SOUL.md").write_text("v2 content", encoding="utf-8") + git_ready.auto_commit("v2") + + commits = git_ready.log() + # commits[0] = v2 (HEAD), commits[1] = init + # Revert v2 → restore to init's state (empty SOUL.md) + new_sha = git_ready.revert(commits[0].sha) + assert new_sha is not None + assert (ws / "SOUL.md").read_text(encoding="utf-8") == "" + + def test_root_commit_returns_none(self, git_ready): + """Cannot revert the root commit (no parent to restore to).""" + commits = git_ready.log() + assert len(commits) == 1 + assert git_ready.revert(commits[0].sha) is None + + def test_invalid_sha_returns_none(self, git_ready): + assert git_ready.revert("deadbeef") is None + + +class TestMemoryStoreGitProperty: + def test_git_property_exposes_gitstore(self, tmp_path): + from nanobot.agent.memory import MemoryStore + store = MemoryStore(tmp_path) + assert isinstance(store.git, GitStore) + + def test_git_property_is_same_object(self, tmp_path): + from nanobot.agent.memory import MemoryStore + store = MemoryStore(tmp_path) + assert store.git is store._git diff --git a/tests/agent/test_hook_composite.py b/tests/agent/test_hook_composite.py index 203c892fb..590d8db64 100644 --- a/tests/agent/test_hook_composite.py +++ b/tests/agent/test_hook_composite.py @@ -249,7 +249,8 @@ def _make_loop(tmp_path, hooks=None): with patch("nanobot.agent.loop.ContextBuilder"), \ patch("nanobot.agent.loop.SessionManager"), \ patch("nanobot.agent.loop.SubagentManager") as mock_sub_mgr, \ - patch("nanobot.agent.loop.MemoryConsolidator"): + patch("nanobot.agent.loop.Consolidator"), \ + patch("nanobot.agent.loop.Dream"): mock_sub_mgr.return_value.cancel_by_session = AsyncMock(return_value=0) loop = AgentLoop( bus=bus, provider=provider, workspace=tmp_path, hooks=hooks, diff --git a/tests/agent/test_loop_consolidation_tokens.py b/tests/agent/test_loop_consolidation_tokens.py index 2f9c2dea7..87e159cc8 100644 --- a/tests/agent/test_loop_consolidation_tokens.py +++ b/tests/agent/test_loop_consolidation_tokens.py @@ -26,24 +26,24 @@ def _make_loop(tmp_path, *, estimated_tokens: int, context_window_tokens: int) - context_window_tokens=context_window_tokens, ) loop.tools.get_definitions = MagicMock(return_value=[]) - loop.memory_consolidator._SAFETY_BUFFER = 0 + loop.consolidator._SAFETY_BUFFER = 0 return loop @pytest.mark.asyncio async def test_prompt_below_threshold_does_not_consolidate(tmp_path) -> None: loop = _make_loop(tmp_path, estimated_tokens=100, context_window_tokens=200) - loop.memory_consolidator.consolidate_messages = AsyncMock(return_value=True) # type: ignore[method-assign] + loop.consolidator.archive = AsyncMock(return_value=True) # type: ignore[method-assign] await loop.process_direct("hello", session_key="cli:test") - loop.memory_consolidator.consolidate_messages.assert_not_awaited() + loop.consolidator.archive.assert_not_awaited() @pytest.mark.asyncio async def test_prompt_above_threshold_triggers_consolidation(tmp_path, monkeypatch) -> None: loop = _make_loop(tmp_path, estimated_tokens=1000, context_window_tokens=200) - loop.memory_consolidator.consolidate_messages = AsyncMock(return_value=True) # type: ignore[method-assign] + loop.consolidator.archive = AsyncMock(return_value=True) # type: ignore[method-assign] session = loop.sessions.get_or_create("cli:test") session.messages = [ {"role": "user", "content": "u1", "timestamp": "2026-01-01T00:00:00"}, @@ -55,13 +55,13 @@ async def test_prompt_above_threshold_triggers_consolidation(tmp_path, monkeypat await loop.process_direct("hello", session_key="cli:test") - assert loop.memory_consolidator.consolidate_messages.await_count >= 1 + assert loop.consolidator.archive.await_count >= 1 @pytest.mark.asyncio async def test_prompt_above_threshold_archives_until_next_user_boundary(tmp_path, monkeypatch) -> None: loop = _make_loop(tmp_path, estimated_tokens=1000, context_window_tokens=200) - loop.memory_consolidator.consolidate_messages = AsyncMock(return_value=True) # type: ignore[method-assign] + loop.consolidator.archive = AsyncMock(return_value=True) # type: ignore[method-assign] session = loop.sessions.get_or_create("cli:test") session.messages = [ @@ -76,9 +76,9 @@ async def test_prompt_above_threshold_archives_until_next_user_boundary(tmp_path token_map = {"u1": 120, "a1": 120, "u2": 120, "a2": 120, "u3": 120} monkeypatch.setattr(memory_module, "estimate_message_tokens", lambda message: token_map[message["content"]]) - await loop.memory_consolidator.maybe_consolidate_by_tokens(session) + await loop.consolidator.maybe_consolidate_by_tokens(session) - archived_chunk = loop.memory_consolidator.consolidate_messages.await_args.args[0] + archived_chunk = loop.consolidator.archive.await_args.args[0] assert [message["content"] for message in archived_chunk] == ["u1", "a1", "u2", "a2"] assert session.last_consolidated == 4 @@ -87,7 +87,7 @@ async def test_prompt_above_threshold_archives_until_next_user_boundary(tmp_path async def test_consolidation_loops_until_target_met(tmp_path, monkeypatch) -> None: """Verify maybe_consolidate_by_tokens keeps looping until under threshold.""" loop = _make_loop(tmp_path, estimated_tokens=0, context_window_tokens=200) - loop.memory_consolidator.consolidate_messages = AsyncMock(return_value=True) # type: ignore[method-assign] + loop.consolidator.archive = AsyncMock(return_value=True) # type: ignore[method-assign] session = loop.sessions.get_or_create("cli:test") session.messages = [ @@ -110,12 +110,12 @@ async def test_consolidation_loops_until_target_met(tmp_path, monkeypatch) -> No return (300, "test") return (80, "test") - loop.memory_consolidator.estimate_session_prompt_tokens = mock_estimate # type: ignore[method-assign] + loop.consolidator.estimate_session_prompt_tokens = mock_estimate # type: ignore[method-assign] monkeypatch.setattr(memory_module, "estimate_message_tokens", lambda _m: 100) - await loop.memory_consolidator.maybe_consolidate_by_tokens(session) + await loop.consolidator.maybe_consolidate_by_tokens(session) - assert loop.memory_consolidator.consolidate_messages.await_count == 2 + assert loop.consolidator.archive.await_count == 2 assert session.last_consolidated == 6 @@ -123,7 +123,7 @@ async def test_consolidation_loops_until_target_met(tmp_path, monkeypatch) -> No async def test_consolidation_continues_below_trigger_until_half_target(tmp_path, monkeypatch) -> None: """Once triggered, consolidation should continue until it drops below half threshold.""" loop = _make_loop(tmp_path, estimated_tokens=0, context_window_tokens=200) - loop.memory_consolidator.consolidate_messages = AsyncMock(return_value=True) # type: ignore[method-assign] + loop.consolidator.archive = AsyncMock(return_value=True) # type: ignore[method-assign] session = loop.sessions.get_or_create("cli:test") session.messages = [ @@ -147,12 +147,12 @@ async def test_consolidation_continues_below_trigger_until_half_target(tmp_path, return (150, "test") return (80, "test") - loop.memory_consolidator.estimate_session_prompt_tokens = mock_estimate # type: ignore[method-assign] + loop.consolidator.estimate_session_prompt_tokens = mock_estimate # type: ignore[method-assign] monkeypatch.setattr(memory_module, "estimate_message_tokens", lambda _m: 100) - await loop.memory_consolidator.maybe_consolidate_by_tokens(session) + await loop.consolidator.maybe_consolidate_by_tokens(session) - assert loop.memory_consolidator.consolidate_messages.await_count == 2 + assert loop.consolidator.archive.await_count == 2 assert session.last_consolidated == 6 @@ -166,7 +166,7 @@ async def test_preflight_consolidation_before_llm_call(tmp_path, monkeypatch) -> async def track_consolidate(messages): order.append("consolidate") return True - loop.memory_consolidator.consolidate_messages = track_consolidate # type: ignore[method-assign] + loop.consolidator.archive = track_consolidate # type: ignore[method-assign] async def track_llm(*args, **kwargs): order.append("llm") @@ -187,7 +187,7 @@ async def test_preflight_consolidation_before_llm_call(tmp_path, monkeypatch) -> def mock_estimate(_session): call_count[0] += 1 return (1000 if call_count[0] <= 1 else 80, "test") - loop.memory_consolidator.estimate_session_prompt_tokens = mock_estimate # type: ignore[method-assign] + loop.consolidator.estimate_session_prompt_tokens = mock_estimate # type: ignore[method-assign] await loop.process_direct("hello", session_key="cli:test") diff --git a/tests/agent/test_memory_consolidation_types.py b/tests/agent/test_memory_consolidation_types.py deleted file mode 100644 index 203e39a90..000000000 --- a/tests/agent/test_memory_consolidation_types.py +++ /dev/null @@ -1,478 +0,0 @@ -"""Test MemoryStore.consolidate() handles non-string tool call arguments. - -Regression test for https://github.com/HKUDS/nanobot/issues/1042 -When memory consolidation receives dict values instead of strings from the LLM -tool call response, it should serialize them to JSON instead of raising TypeError. -""" - -import json -from pathlib import Path -from unittest.mock import AsyncMock - -import pytest - -from nanobot.agent.memory import MemoryStore -from nanobot.providers.base import LLMProvider, LLMResponse, ToolCallRequest - - -def _make_messages(message_count: int = 30): - """Create a list of mock messages.""" - return [ - {"role": "user", "content": f"msg{i}", "timestamp": "2026-01-01 00:00"} - for i in range(message_count) - ] - - -def _make_tool_response(history_entry, memory_update): - """Create an LLMResponse with a save_memory tool call.""" - return LLMResponse( - content=None, - tool_calls=[ - ToolCallRequest( - id="call_1", - name="save_memory", - arguments={ - "history_entry": history_entry, - "memory_update": memory_update, - }, - ) - ], - ) - - -class ScriptedProvider(LLMProvider): - def __init__(self, responses: list[LLMResponse]): - super().__init__() - self._responses = list(responses) - self.calls = 0 - - async def chat(self, *args, **kwargs) -> LLMResponse: - self.calls += 1 - if self._responses: - return self._responses.pop(0) - return LLMResponse(content="", tool_calls=[]) - - def get_default_model(self) -> str: - return "test-model" - - -class TestMemoryConsolidationTypeHandling: - """Test that consolidation handles various argument types correctly.""" - - @pytest.mark.asyncio - async def test_string_arguments_work(self, tmp_path: Path) -> None: - """Normal case: LLM returns string arguments.""" - store = MemoryStore(tmp_path) - provider = AsyncMock() - provider.chat = AsyncMock( - return_value=_make_tool_response( - history_entry="[2026-01-01] User discussed testing.", - memory_update="# Memory\nUser likes testing.", - ) - ) - provider.chat_with_retry = provider.chat - messages = _make_messages(message_count=60) - - result = await store.consolidate(messages, provider, "test-model") - - assert result is True - assert store.history_file.exists() - assert "[2026-01-01] User discussed testing." in store.history_file.read_text() - assert "User likes testing." in store.memory_file.read_text() - - @pytest.mark.asyncio - async def test_dict_arguments_serialized_to_json(self, tmp_path: Path) -> None: - """Issue #1042: LLM returns dict instead of string — must not raise TypeError.""" - store = MemoryStore(tmp_path) - provider = AsyncMock() - provider.chat = AsyncMock( - return_value=_make_tool_response( - history_entry={"timestamp": "2026-01-01", "summary": "User discussed testing."}, - memory_update={"facts": ["User likes testing"], "topics": ["testing"]}, - ) - ) - provider.chat_with_retry = provider.chat - messages = _make_messages(message_count=60) - - result = await store.consolidate(messages, provider, "test-model") - - assert result is True - assert store.history_file.exists() - history_content = store.history_file.read_text() - parsed = json.loads(history_content.strip()) - assert parsed["summary"] == "User discussed testing." - - memory_content = store.memory_file.read_text() - parsed_mem = json.loads(memory_content) - assert "User likes testing" in parsed_mem["facts"] - - @pytest.mark.asyncio - async def test_string_arguments_as_raw_json(self, tmp_path: Path) -> None: - """Some providers return arguments as a JSON string instead of parsed dict.""" - store = MemoryStore(tmp_path) - provider = AsyncMock() - - response = LLMResponse( - content=None, - tool_calls=[ - ToolCallRequest( - id="call_1", - name="save_memory", - arguments=json.dumps({ - "history_entry": "[2026-01-01] User discussed testing.", - "memory_update": "# Memory\nUser likes testing.", - }), - ) - ], - ) - provider.chat = AsyncMock(return_value=response) - provider.chat_with_retry = provider.chat - messages = _make_messages(message_count=60) - - result = await store.consolidate(messages, provider, "test-model") - - assert result is True - assert "User discussed testing." in store.history_file.read_text() - - @pytest.mark.asyncio - async def test_no_tool_call_returns_false(self, tmp_path: Path) -> None: - """When LLM doesn't use the save_memory tool, return False.""" - store = MemoryStore(tmp_path) - provider = AsyncMock() - provider.chat = AsyncMock( - return_value=LLMResponse(content="I summarized the conversation.", tool_calls=[]) - ) - provider.chat_with_retry = provider.chat - messages = _make_messages(message_count=60) - - result = await store.consolidate(messages, provider, "test-model") - - assert result is False - assert not store.history_file.exists() - - @pytest.mark.asyncio - async def test_skips_when_message_chunk_is_empty(self, tmp_path: Path) -> None: - """Consolidation should be a no-op when the selected chunk is empty.""" - store = MemoryStore(tmp_path) - provider = AsyncMock() - provider.chat_with_retry = provider.chat - messages: list[dict] = [] - - result = await store.consolidate(messages, provider, "test-model") - - assert result is True - provider.chat.assert_not_called() - - @pytest.mark.asyncio - async def test_list_arguments_extracts_first_dict(self, tmp_path: Path) -> None: - """Some providers return arguments as a list - extract first element if it's a dict.""" - store = MemoryStore(tmp_path) - provider = AsyncMock() - - response = LLMResponse( - content=None, - tool_calls=[ - ToolCallRequest( - id="call_1", - name="save_memory", - arguments=[{ - "history_entry": "[2026-01-01] User discussed testing.", - "memory_update": "# Memory\nUser likes testing.", - }], - ) - ], - ) - provider.chat = AsyncMock(return_value=response) - provider.chat_with_retry = provider.chat - messages = _make_messages(message_count=60) - - result = await store.consolidate(messages, provider, "test-model") - - assert result is True - assert "User discussed testing." in store.history_file.read_text() - assert "User likes testing." in store.memory_file.read_text() - - @pytest.mark.asyncio - async def test_list_arguments_empty_list_returns_false(self, tmp_path: Path) -> None: - """Empty list arguments should return False.""" - store = MemoryStore(tmp_path) - provider = AsyncMock() - - response = LLMResponse( - content=None, - tool_calls=[ - ToolCallRequest( - id="call_1", - name="save_memory", - arguments=[], - ) - ], - ) - provider.chat = AsyncMock(return_value=response) - provider.chat_with_retry = provider.chat - messages = _make_messages(message_count=60) - - result = await store.consolidate(messages, provider, "test-model") - - assert result is False - - @pytest.mark.asyncio - async def test_list_arguments_non_dict_content_returns_false(self, tmp_path: Path) -> None: - """List with non-dict content should return False.""" - store = MemoryStore(tmp_path) - provider = AsyncMock() - - response = LLMResponse( - content=None, - tool_calls=[ - ToolCallRequest( - id="call_1", - name="save_memory", - arguments=["string", "content"], - ) - ], - ) - provider.chat = AsyncMock(return_value=response) - provider.chat_with_retry = provider.chat - messages = _make_messages(message_count=60) - - result = await store.consolidate(messages, provider, "test-model") - - assert result is False - - @pytest.mark.asyncio - async def test_missing_history_entry_returns_false_without_writing(self, tmp_path: Path) -> None: - """Do not persist partial results when required fields are missing.""" - store = MemoryStore(tmp_path) - provider = AsyncMock() - provider.chat_with_retry = AsyncMock( - return_value=LLMResponse( - content=None, - tool_calls=[ - ToolCallRequest( - id="call_1", - name="save_memory", - arguments={"memory_update": "# Memory\nOnly memory update"}, - ) - ], - ) - ) - messages = _make_messages(message_count=60) - - result = await store.consolidate(messages, provider, "test-model") - - assert result is False - assert not store.history_file.exists() - assert not store.memory_file.exists() - - @pytest.mark.asyncio - async def test_missing_memory_update_returns_false_without_writing(self, tmp_path: Path) -> None: - """Do not append history if memory_update is missing.""" - store = MemoryStore(tmp_path) - provider = AsyncMock() - provider.chat_with_retry = AsyncMock( - return_value=LLMResponse( - content=None, - tool_calls=[ - ToolCallRequest( - id="call_1", - name="save_memory", - arguments={"history_entry": "[2026-01-01] Partial output."}, - ) - ], - ) - ) - messages = _make_messages(message_count=60) - - result = await store.consolidate(messages, provider, "test-model") - - assert result is False - assert not store.history_file.exists() - assert not store.memory_file.exists() - - @pytest.mark.asyncio - async def test_null_required_field_returns_false_without_writing(self, tmp_path: Path) -> None: - """Null required fields should be rejected before persistence.""" - store = MemoryStore(tmp_path) - provider = AsyncMock() - provider.chat_with_retry = AsyncMock( - return_value=_make_tool_response( - history_entry=None, - memory_update="# Memory\nUser likes testing.", - ) - ) - messages = _make_messages(message_count=60) - - result = await store.consolidate(messages, provider, "test-model") - - assert result is False - assert not store.history_file.exists() - assert not store.memory_file.exists() - - @pytest.mark.asyncio - async def test_empty_history_entry_returns_false_without_writing(self, tmp_path: Path) -> None: - """Empty history entries should be rejected to avoid blank archival records.""" - store = MemoryStore(tmp_path) - provider = AsyncMock() - provider.chat_with_retry = AsyncMock( - return_value=_make_tool_response( - history_entry=" ", - memory_update="# Memory\nUser likes testing.", - ) - ) - messages = _make_messages(message_count=60) - - result = await store.consolidate(messages, provider, "test-model") - - assert result is False - assert not store.history_file.exists() - assert not store.memory_file.exists() - - @pytest.mark.asyncio - async def test_retries_transient_error_then_succeeds(self, tmp_path: Path, monkeypatch) -> None: - store = MemoryStore(tmp_path) - provider = ScriptedProvider([ - LLMResponse(content="503 server error", finish_reason="error"), - _make_tool_response( - history_entry="[2026-01-01] User discussed testing.", - memory_update="# Memory\nUser likes testing.", - ), - ]) - messages = _make_messages(message_count=60) - delays: list[int] = [] - - async def _fake_sleep(delay: int) -> None: - delays.append(delay) - - monkeypatch.setattr("nanobot.providers.base.asyncio.sleep", _fake_sleep) - - result = await store.consolidate(messages, provider, "test-model") - - assert result is True - assert provider.calls == 2 - assert delays == [1] - - @pytest.mark.asyncio - async def test_consolidation_delegates_to_provider_defaults(self, tmp_path: Path) -> None: - """Consolidation no longer passes generation params — the provider owns them.""" - store = MemoryStore(tmp_path) - provider = AsyncMock() - provider.chat_with_retry = AsyncMock( - return_value=_make_tool_response( - history_entry="[2026-01-01] User discussed testing.", - memory_update="# Memory\nUser likes testing.", - ) - ) - messages = _make_messages(message_count=60) - - result = await store.consolidate(messages, provider, "test-model") - - assert result is True - provider.chat_with_retry.assert_awaited_once() - _, kwargs = provider.chat_with_retry.await_args - assert kwargs["model"] == "test-model" - assert "temperature" not in kwargs - assert "max_tokens" not in kwargs - assert "reasoning_effort" not in kwargs - - @pytest.mark.asyncio - async def test_tool_choice_fallback_on_unsupported_error(self, tmp_path: Path) -> None: - """Forced tool_choice rejected by provider -> retry with auto and succeed.""" - store = MemoryStore(tmp_path) - error_resp = LLMResponse( - content="Error calling LLM: BadRequestError: " - "The tool_choice parameter does not support being set to required or object", - finish_reason="error", - tool_calls=[], - ) - ok_resp = _make_tool_response( - history_entry="[2026-01-01] Fallback worked.", - memory_update="# Memory\nFallback OK.", - ) - - call_log: list[dict] = [] - - async def _tracking_chat(**kwargs): - call_log.append(kwargs) - return error_resp if len(call_log) == 1 else ok_resp - - provider = AsyncMock() - provider.chat_with_retry = AsyncMock(side_effect=_tracking_chat) - messages = _make_messages(message_count=60) - - result = await store.consolidate(messages, provider, "test-model") - - assert result is True - assert len(call_log) == 2 - assert isinstance(call_log[0]["tool_choice"], dict) - assert call_log[1]["tool_choice"] == "auto" - assert "Fallback worked." in store.history_file.read_text() - - @pytest.mark.asyncio - async def test_tool_choice_fallback_auto_no_tool_call(self, tmp_path: Path) -> None: - """Forced rejected, auto retry also produces no tool call -> return False.""" - store = MemoryStore(tmp_path) - error_resp = LLMResponse( - content="Error: tool_choice must be none or auto", - finish_reason="error", - tool_calls=[], - ) - no_tool_resp = LLMResponse( - content="Here is a summary.", - finish_reason="stop", - tool_calls=[], - ) - - provider = AsyncMock() - provider.chat_with_retry = AsyncMock(side_effect=[error_resp, no_tool_resp]) - messages = _make_messages(message_count=60) - - result = await store.consolidate(messages, provider, "test-model") - - assert result is False - assert not store.history_file.exists() - - @pytest.mark.asyncio - async def test_raw_archive_after_consecutive_failures(self, tmp_path: Path) -> None: - """After 3 consecutive failures, raw-archive messages and return True.""" - store = MemoryStore(tmp_path) - no_tool = LLMResponse(content="No tool call.", finish_reason="stop", tool_calls=[]) - provider = AsyncMock() - provider.chat_with_retry = AsyncMock(return_value=no_tool) - messages = _make_messages(message_count=10) - - assert await store.consolidate(messages, provider, "m") is False - assert await store.consolidate(messages, provider, "m") is False - assert await store.consolidate(messages, provider, "m") is True - - assert store.history_file.exists() - content = store.history_file.read_text() - assert "[RAW]" in content - assert "10 messages" in content - assert "msg0" in content - assert not store.memory_file.exists() - - @pytest.mark.asyncio - async def test_raw_archive_counter_resets_on_success(self, tmp_path: Path) -> None: - """A successful consolidation resets the failure counter.""" - store = MemoryStore(tmp_path) - no_tool = LLMResponse(content="Nope.", finish_reason="stop", tool_calls=[]) - ok_resp = _make_tool_response( - history_entry="[2026-01-01] OK.", - memory_update="# Memory\nOK.", - ) - messages = _make_messages(message_count=10) - - provider = AsyncMock() - provider.chat_with_retry = AsyncMock(return_value=no_tool) - assert await store.consolidate(messages, provider, "m") is False - assert await store.consolidate(messages, provider, "m") is False - assert store._consecutive_failures == 2 - - provider.chat_with_retry = AsyncMock(return_value=ok_resp) - assert await store.consolidate(messages, provider, "m") is True - assert store._consecutive_failures == 0 - - provider.chat_with_retry = AsyncMock(return_value=no_tool) - assert await store.consolidate(messages, provider, "m") is False - assert store._consecutive_failures == 1 diff --git a/tests/agent/test_memory_store.py b/tests/agent/test_memory_store.py new file mode 100644 index 000000000..efe7d198e --- /dev/null +++ b/tests/agent/test_memory_store.py @@ -0,0 +1,267 @@ +"""Tests for the restructured MemoryStore — pure file I/O layer.""" + +from datetime import datetime +import json +from pathlib import Path + +import pytest + +from nanobot.agent.memory import MemoryStore + + +@pytest.fixture +def store(tmp_path): + return MemoryStore(tmp_path) + + +class TestMemoryStoreBasicIO: + def test_read_memory_returns_empty_when_missing(self, store): + assert store.read_memory() == "" + + def test_write_and_read_memory(self, store): + store.write_memory("hello") + assert store.read_memory() == "hello" + + def test_read_soul_returns_empty_when_missing(self, store): + assert store.read_soul() == "" + + def test_write_and_read_soul(self, store): + store.write_soul("soul content") + assert store.read_soul() == "soul content" + + def test_read_user_returns_empty_when_missing(self, store): + assert store.read_user() == "" + + def test_write_and_read_user(self, store): + store.write_user("user content") + assert store.read_user() == "user content" + + def test_get_memory_context_returns_empty_when_missing(self, store): + assert store.get_memory_context() == "" + + def test_get_memory_context_returns_formatted_content(self, store): + store.write_memory("important fact") + ctx = store.get_memory_context() + assert "Long-term Memory" in ctx + assert "important fact" in ctx + + +class TestHistoryWithCursor: + def test_append_history_returns_cursor(self, store): + cursor = store.append_history("event 1") + assert cursor == 1 + cursor2 = store.append_history("event 2") + assert cursor2 == 2 + + def test_append_history_includes_cursor_in_file(self, store): + store.append_history("event 1") + content = store.read_file(store.history_file) + data = json.loads(content) + assert data["cursor"] == 1 + + def test_cursor_persists_across_appends(self, store): + store.append_history("event 1") + store.append_history("event 2") + cursor = store.append_history("event 3") + assert cursor == 3 + + def test_read_unprocessed_history(self, store): + store.append_history("event 1") + store.append_history("event 2") + store.append_history("event 3") + entries = store.read_unprocessed_history(since_cursor=1) + assert len(entries) == 2 + assert entries[0]["cursor"] == 2 + + def test_read_unprocessed_history_returns_all_when_cursor_zero(self, store): + store.append_history("event 1") + store.append_history("event 2") + entries = store.read_unprocessed_history(since_cursor=0) + assert len(entries) == 2 + + def test_compact_history_drops_oldest(self, tmp_path): + store = MemoryStore(tmp_path, max_history_entries=2) + store.append_history("event 1") + store.append_history("event 2") + store.append_history("event 3") + store.append_history("event 4") + store.append_history("event 5") + store.compact_history() + entries = store.read_unprocessed_history(since_cursor=0) + assert len(entries) == 2 + assert entries[0]["cursor"] in {4, 5} + + +class TestDreamCursor: + def test_initial_cursor_is_zero(self, store): + assert store.get_last_dream_cursor() == 0 + + def test_set_and_get_cursor(self, store): + store.set_last_dream_cursor(5) + assert store.get_last_dream_cursor() == 5 + + def test_cursor_persists(self, store): + store.set_last_dream_cursor(3) + store2 = MemoryStore(store.workspace) + assert store2.get_last_dream_cursor() == 3 + + +class TestLegacyHistoryMigration: + def test_read_unprocessed_history_handles_entries_without_cursor(self, store): + """JSONL entries with cursor=1 are correctly parsed and returned.""" + store.history_file.write_text( + '{"cursor": 1, "timestamp": "2026-03-30 14:30", "content": "Old event"}\n', + encoding="utf-8") + entries = store.read_unprocessed_history(since_cursor=0) + assert len(entries) == 1 + assert entries[0]["cursor"] == 1 + + def test_migrates_legacy_history_md_preserving_partial_entries(self, tmp_path): + memory_dir = tmp_path / "memory" + memory_dir.mkdir() + legacy_file = memory_dir / "HISTORY.md" + legacy_content = ( + "[2026-04-01 10:00] User prefers dark mode.\n\n" + "[2026-04-01 10:05] [RAW] 2 messages\n" + "[2026-04-01 10:04] USER: hello\n" + "[2026-04-01 10:04] ASSISTANT: hi\n\n" + "Legacy chunk without timestamp.\n" + "Keep whatever content we can recover.\n" + ) + legacy_file.write_text(legacy_content, encoding="utf-8") + + store = MemoryStore(tmp_path) + fallback_timestamp = datetime.fromtimestamp( + (memory_dir / "HISTORY.md.bak").stat().st_mtime, + ).strftime("%Y-%m-%d %H:%M") + + entries = store.read_unprocessed_history(since_cursor=0) + assert [entry["cursor"] for entry in entries] == [1, 2, 3] + assert entries[0]["timestamp"] == "2026-04-01 10:00" + assert entries[0]["content"] == "User prefers dark mode." + assert entries[1]["timestamp"] == "2026-04-01 10:05" + assert entries[1]["content"].startswith("[RAW] 2 messages") + assert "USER: hello" in entries[1]["content"] + assert entries[2]["timestamp"] == fallback_timestamp + assert entries[2]["content"].startswith("Legacy chunk without timestamp.") + assert store.read_file(store._cursor_file).strip() == "3" + assert store.read_file(store._dream_cursor_file).strip() == "3" + assert not legacy_file.exists() + assert (memory_dir / "HISTORY.md.bak").read_text(encoding="utf-8") == legacy_content + + def test_migrates_consecutive_entries_without_blank_lines(self, tmp_path): + memory_dir = tmp_path / "memory" + memory_dir.mkdir() + legacy_file = memory_dir / "HISTORY.md" + legacy_content = ( + "[2026-04-01 10:00] First event.\n" + "[2026-04-01 10:01] Second event.\n" + "[2026-04-01 10:02] Third event.\n" + ) + legacy_file.write_text(legacy_content, encoding="utf-8") + + store = MemoryStore(tmp_path) + + entries = store.read_unprocessed_history(since_cursor=0) + assert len(entries) == 3 + assert [entry["content"] for entry in entries] == [ + "First event.", + "Second event.", + "Third event.", + ] + + def test_raw_archive_stays_single_entry_while_following_events_split(self, tmp_path): + memory_dir = tmp_path / "memory" + memory_dir.mkdir() + legacy_file = memory_dir / "HISTORY.md" + legacy_content = ( + "[2026-04-01 10:05] [RAW] 2 messages\n" + "[2026-04-01 10:04] USER: hello\n" + "[2026-04-01 10:04] ASSISTANT: hi\n" + "[2026-04-01 10:06] Normal event after raw block.\n" + ) + legacy_file.write_text(legacy_content, encoding="utf-8") + + store = MemoryStore(tmp_path) + + entries = store.read_unprocessed_history(since_cursor=0) + assert len(entries) == 2 + assert entries[0]["content"].startswith("[RAW] 2 messages") + assert "USER: hello" in entries[0]["content"] + assert entries[1]["content"] == "Normal event after raw block." + + def test_nonstandard_date_headers_still_start_new_entries(self, tmp_path): + memory_dir = tmp_path / "memory" + memory_dir.mkdir() + legacy_file = memory_dir / "HISTORY.md" + legacy_content = ( + "[2026-03-25–2026-04-02] Multi-day summary.\n" + "[2026-03-26/27] Cross-day summary.\n" + ) + legacy_file.write_text(legacy_content, encoding="utf-8") + + store = MemoryStore(tmp_path) + fallback_timestamp = datetime.fromtimestamp( + (memory_dir / "HISTORY.md.bak").stat().st_mtime, + ).strftime("%Y-%m-%d %H:%M") + + entries = store.read_unprocessed_history(since_cursor=0) + assert len(entries) == 2 + assert entries[0]["timestamp"] == fallback_timestamp + assert entries[0]["content"] == "[2026-03-25–2026-04-02] Multi-day summary." + assert entries[1]["timestamp"] == fallback_timestamp + assert entries[1]["content"] == "[2026-03-26/27] Cross-day summary." + + def test_existing_history_jsonl_skips_legacy_migration(self, tmp_path): + memory_dir = tmp_path / "memory" + memory_dir.mkdir() + history_file = memory_dir / "history.jsonl" + history_file.write_text( + '{"cursor": 7, "timestamp": "2026-04-01 12:00", "content": "existing"}\n', + encoding="utf-8", + ) + legacy_file = memory_dir / "HISTORY.md" + legacy_file.write_text("[2026-04-01 10:00] legacy\n\n", encoding="utf-8") + + store = MemoryStore(tmp_path) + + entries = store.read_unprocessed_history(since_cursor=0) + assert len(entries) == 1 + assert entries[0]["cursor"] == 7 + assert entries[0]["content"] == "existing" + assert legacy_file.exists() + assert not (memory_dir / "HISTORY.md.bak").exists() + + def test_empty_history_jsonl_still_allows_legacy_migration(self, tmp_path): + memory_dir = tmp_path / "memory" + memory_dir.mkdir() + history_file = memory_dir / "history.jsonl" + history_file.write_text("", encoding="utf-8") + legacy_file = memory_dir / "HISTORY.md" + legacy_file.write_text("[2026-04-01 10:00] legacy\n\n", encoding="utf-8") + + store = MemoryStore(tmp_path) + + entries = store.read_unprocessed_history(since_cursor=0) + assert len(entries) == 1 + assert entries[0]["cursor"] == 1 + assert entries[0]["timestamp"] == "2026-04-01 10:00" + assert entries[0]["content"] == "legacy" + assert not legacy_file.exists() + assert (memory_dir / "HISTORY.md.bak").exists() + + def test_migrates_legacy_history_with_invalid_utf8_bytes(self, tmp_path): + memory_dir = tmp_path / "memory" + memory_dir.mkdir() + legacy_file = memory_dir / "HISTORY.md" + legacy_file.write_bytes( + b"[2026-04-01 10:00] Broken \xff data still needs migration.\n\n" + ) + + store = MemoryStore(tmp_path) + + entries = store.read_unprocessed_history(since_cursor=0) + assert len(entries) == 1 + assert entries[0]["timestamp"] == "2026-04-01 10:00" + assert "Broken" in entries[0]["content"] + assert "migration." in entries[0]["content"] diff --git a/tests/channels/test_telegram_channel.py b/tests/channels/test_telegram_channel.py index c793b1224..b5e74152b 100644 --- a/tests/channels/test_telegram_channel.py +++ b/tests/channels/test_telegram_channel.py @@ -185,6 +185,9 @@ async def test_start_creates_separate_pools_with_proxy(monkeypatch) -> None: assert builder.request_value is api_req assert builder.get_updates_request_value is poll_req assert any(cmd.command == "status" for cmd in app.bot.commands) + assert any(cmd.command == "dream" for cmd in app.bot.commands) + assert any(cmd.command == "dream-log" for cmd in app.bot.commands) + assert any(cmd.command == "dream-restore" for cmd in app.bot.commands) @pytest.mark.asyncio @@ -962,6 +965,27 @@ async def test_forward_command_does_not_inject_reply_context() -> None: assert handled[0]["content"] == "/new" +@pytest.mark.asyncio +async def test_forward_command_preserves_dream_log_args_and_strips_bot_suffix() -> None: + channel = TelegramChannel( + TelegramConfig(enabled=True, token="123:abc", allow_from=["*"], group_policy="open"), + MessageBus(), + ) + channel._app = _FakeApp(lambda: None) + handled = [] + + async def capture_handle(**kwargs) -> None: + handled.append(kwargs) + + channel._handle_message = capture_handle + update = _make_telegram_update(text="/dream-log@nanobot_test deadbeef", reply_to_message=None) + + await channel._forward_command(update, None) + + assert len(handled) == 1 + assert handled[0]["content"] == "/dream-log deadbeef" + + @pytest.mark.asyncio async def test_on_help_includes_restart_command() -> None: channel = TelegramChannel( @@ -977,3 +1001,6 @@ async def test_on_help_includes_restart_command() -> None: help_text = update.message.reply_text.await_args.args[0] assert "/restart" in help_text assert "/status" in help_text + assert "/dream" in help_text + assert "/dream-log" in help_text + assert "/dream-restore" in help_text diff --git a/tests/cli/test_restart_command.py b/tests/cli/test_restart_command.py index 8ea30f684..8b079d4e7 100644 --- a/tests/cli/test_restart_command.py +++ b/tests/cli/test_restart_command.py @@ -137,7 +137,7 @@ class TestRestartCommand: loop.sessions.get_or_create.return_value = session loop._start_time = time.time() - 125 loop._last_usage = {"prompt_tokens": 0, "completion_tokens": 0} - loop.memory_consolidator.estimate_session_prompt_tokens = MagicMock( + loop.consolidator.estimate_session_prompt_tokens = MagicMock( return_value=(20500, "tiktoken") ) @@ -176,7 +176,7 @@ class TestRestartCommand: session.get_history.return_value = [{"role": "user"}] loop.sessions.get_or_create.return_value = session loop._last_usage = {"prompt_tokens": 1200, "completion_tokens": 34} - loop.memory_consolidator.estimate_session_prompt_tokens = MagicMock( + loop.consolidator.estimate_session_prompt_tokens = MagicMock( return_value=(0, "none") ) diff --git a/tests/command/test_builtin_dream.py b/tests/command/test_builtin_dream.py new file mode 100644 index 000000000..7b1835feb --- /dev/null +++ b/tests/command/test_builtin_dream.py @@ -0,0 +1,143 @@ +from __future__ import annotations + +from types import SimpleNamespace + +import pytest + +from nanobot.bus.events import InboundMessage +from nanobot.command.builtin import cmd_dream_log, cmd_dream_restore +from nanobot.command.router import CommandContext +from nanobot.utils.gitstore import CommitInfo + + +class _FakeStore: + def __init__(self, git, last_dream_cursor: int = 1): + self.git = git + self._last_dream_cursor = last_dream_cursor + + def get_last_dream_cursor(self) -> int: + return self._last_dream_cursor + + +class _FakeGit: + def __init__( + self, + *, + initialized: bool = True, + commits: list[CommitInfo] | None = None, + diff_map: dict[str, tuple[CommitInfo, str] | None] | None = None, + revert_result: str | None = None, + ): + self._initialized = initialized + self._commits = commits or [] + self._diff_map = diff_map or {} + self._revert_result = revert_result + + def is_initialized(self) -> bool: + return self._initialized + + def log(self, max_entries: int = 20) -> list[CommitInfo]: + return self._commits[:max_entries] + + def show_commit_diff(self, sha: str, max_entries: int = 20): + return self._diff_map.get(sha) + + def revert(self, sha: str) -> str | None: + return self._revert_result + + +def _make_ctx(raw: str, git: _FakeGit, *, args: str = "", last_dream_cursor: int = 1) -> CommandContext: + msg = InboundMessage(channel="cli", sender_id="u1", chat_id="direct", content=raw) + store = _FakeStore(git, last_dream_cursor=last_dream_cursor) + loop = SimpleNamespace(consolidator=SimpleNamespace(store=store)) + return CommandContext(msg=msg, session=None, key=msg.session_key, raw=raw, args=args, loop=loop) + + +@pytest.mark.asyncio +async def test_dream_log_latest_is_more_user_friendly() -> None: + commit = CommitInfo(sha="abcd1234", message="dream: 2026-04-04, 2 change(s)", timestamp="2026-04-04 12:00") + diff = ( + "diff --git a/SOUL.md b/SOUL.md\n" + "--- a/SOUL.md\n" + "+++ b/SOUL.md\n" + "@@ -1 +1 @@\n" + "-old\n" + "+new\n" + ) + git = _FakeGit(commits=[commit], diff_map={commit.sha: (commit, diff)}) + + out = await cmd_dream_log(_make_ctx("/dream-log", git)) + + assert "## Dream Update" in out.content + assert "Here is the latest Dream memory change." in out.content + assert "- Commit: `abcd1234`" in out.content + assert "- Changed files: `SOUL.md`" in out.content + assert "Use `/dream-restore abcd1234` to undo this change." in out.content + assert "```diff" in out.content + + +@pytest.mark.asyncio +async def test_dream_log_missing_commit_guides_user() -> None: + git = _FakeGit(diff_map={}) + + out = await cmd_dream_log(_make_ctx("/dream-log deadbeef", git, args="deadbeef")) + + assert "Couldn't find Dream change `deadbeef`." in out.content + assert "Use `/dream-restore` to list recent versions" in out.content + + +@pytest.mark.asyncio +async def test_dream_log_before_first_run_is_clear() -> None: + git = _FakeGit(initialized=False) + + out = await cmd_dream_log(_make_ctx("/dream-log", git, last_dream_cursor=0)) + + assert "Dream has not run yet." in out.content + assert "Run `/dream`" in out.content + + +@pytest.mark.asyncio +async def test_dream_restore_lists_versions_with_next_steps() -> None: + commits = [ + CommitInfo(sha="abcd1234", message="dream: latest", timestamp="2026-04-04 12:00"), + CommitInfo(sha="bbbb2222", message="dream: older", timestamp="2026-04-04 08:00"), + ] + git = _FakeGit(commits=commits) + + out = await cmd_dream_restore(_make_ctx("/dream-restore", git)) + + assert "## Dream Restore" in out.content + assert "Choose a Dream memory version to restore." in out.content + assert "`abcd1234` 2026-04-04 12:00 - dream: latest" in out.content + assert "Preview a version with `/dream-log `" in out.content + assert "Restore a version with `/dream-restore `." in out.content + + +@pytest.mark.asyncio +async def test_dream_restore_success_mentions_files_and_followup() -> None: + commit = CommitInfo(sha="abcd1234", message="dream: latest", timestamp="2026-04-04 12:00") + diff = ( + "diff --git a/SOUL.md b/SOUL.md\n" + "--- a/SOUL.md\n" + "+++ b/SOUL.md\n" + "@@ -1 +1 @@\n" + "-old\n" + "+new\n" + "diff --git a/memory/MEMORY.md b/memory/MEMORY.md\n" + "--- a/memory/MEMORY.md\n" + "+++ b/memory/MEMORY.md\n" + "@@ -1 +1 @@\n" + "-old\n" + "+new\n" + ) + git = _FakeGit( + diff_map={commit.sha: (commit, diff)}, + revert_result="eeee9999", + ) + + out = await cmd_dream_restore(_make_ctx("/dream-restore abcd1234", git, args="abcd1234")) + + assert "Restored Dream memory to the state before `abcd1234`." in out.content + assert "- New safety commit: `eeee9999`" in out.content + assert "- Restored files: `SOUL.md`, `memory/MEMORY.md`" in out.content + assert "Use `/dream-log eeee9999` to inspect the restore diff." in out.content diff --git a/tests/config/test_dream_config.py b/tests/config/test_dream_config.py new file mode 100644 index 000000000..9266792bf --- /dev/null +++ b/tests/config/test_dream_config.py @@ -0,0 +1,48 @@ +from nanobot.config.schema import DreamConfig + + +def test_dream_config_defaults_to_interval_hours() -> None: + cfg = DreamConfig() + + assert cfg.interval_h == 2 + assert cfg.cron is None + + +def test_dream_config_builds_every_schedule_from_interval() -> None: + cfg = DreamConfig(interval_h=3) + + schedule = cfg.build_schedule("UTC") + + assert schedule.kind == "every" + assert schedule.every_ms == 3 * 3_600_000 + assert schedule.expr is None + + +def test_dream_config_honors_legacy_cron_override() -> None: + cfg = DreamConfig.model_validate({"cron": "0 */4 * * *"}) + + schedule = cfg.build_schedule("UTC") + + assert schedule.kind == "cron" + assert schedule.expr == "0 */4 * * *" + assert schedule.tz == "UTC" + assert cfg.describe_schedule() == "cron 0 */4 * * * (legacy)" + + +def test_dream_config_dump_uses_interval_h_and_hides_legacy_cron() -> None: + cfg = DreamConfig.model_validate({"intervalH": 5, "cron": "0 */4 * * *"}) + + dumped = cfg.model_dump(by_alias=True) + + assert dumped["intervalH"] == 5 + assert "cron" not in dumped + + +def test_dream_config_uses_model_override_name_and_accepts_legacy_model() -> None: + cfg = DreamConfig.model_validate({"model": "openrouter/sonnet"}) + + dumped = cfg.model_dump(by_alias=True) + + assert cfg.model_override == "openrouter/sonnet" + assert dumped["modelOverride"] == "openrouter/sonnet" + assert "model" not in dumped diff --git a/tests/cron/test_cron_service.py b/tests/cron/test_cron_service.py index 175c5eb9f..76ec4e5be 100644 --- a/tests/cron/test_cron_service.py +++ b/tests/cron/test_cron_service.py @@ -4,7 +4,7 @@ import json import pytest from nanobot.cron.service import CronService -from nanobot.cron.types import CronSchedule +from nanobot.cron.types import CronJob, CronPayload, CronSchedule def test_add_job_rejects_unknown_timezone(tmp_path) -> None: @@ -141,3 +141,18 @@ async def test_running_service_honors_external_disable(tmp_path) -> None: assert called == [] finally: service.stop() + + +def test_remove_job_refuses_system_jobs(tmp_path) -> None: + service = CronService(tmp_path / "cron" / "jobs.json") + service.register_system_job(CronJob( + id="dream", + name="dream", + schedule=CronSchedule(kind="cron", expr="0 */2 * * *", tz="UTC"), + payload=CronPayload(kind="system_event"), + )) + + result = service.remove_job("dream") + + assert result == "protected" + assert service.get_job("dream") is not None diff --git a/tests/cron/test_cron_tool_list.py b/tests/cron/test_cron_tool_list.py index 42ad7d419..5da3f4891 100644 --- a/tests/cron/test_cron_tool_list.py +++ b/tests/cron/test_cron_tool_list.py @@ -4,7 +4,7 @@ from datetime import datetime, timezone from nanobot.agent.tools.cron import CronTool from nanobot.cron.service import CronService -from nanobot.cron.types import CronJobState, CronSchedule +from nanobot.cron.types import CronJob, CronJobState, CronPayload, CronSchedule def _make_tool(tmp_path) -> CronTool: @@ -262,6 +262,39 @@ def test_list_shows_next_run(tmp_path) -> None: assert "(UTC)" in result +def test_list_includes_protected_dream_system_job_with_memory_purpose(tmp_path) -> None: + tool = _make_tool(tmp_path) + tool._cron.register_system_job(CronJob( + id="dream", + name="dream", + schedule=CronSchedule(kind="cron", expr="0 */2 * * *", tz="UTC"), + payload=CronPayload(kind="system_event"), + )) + + result = tool._list_jobs() + + assert "- dream (id: dream, cron: 0 */2 * * * (UTC))" in result + assert "Dream memory consolidation for long-term memory." in result + assert "cannot be removed" in result + + +def test_remove_protected_dream_job_returns_clear_feedback(tmp_path) -> None: + tool = _make_tool(tmp_path) + tool._cron.register_system_job(CronJob( + id="dream", + name="dream", + schedule=CronSchedule(kind="cron", expr="0 */2 * * *", tz="UTC"), + payload=CronPayload(kind="system_event"), + )) + + result = tool._remove_job("dream") + + assert "Cannot remove job `dream`." in result + assert "Dream memory consolidation job for long-term memory" in result + assert "cannot be removed" in result + assert tool._cron.get_job("dream") is not None + + def test_add_cron_job_defaults_to_tool_timezone(tmp_path) -> None: tool = _make_tool_with_tz(tmp_path, "Asia/Shanghai") tool.set_context("telegram", "chat-1")