Your lightweight open-source personal agent.
Python agent-frameworkai-agentai-agentschatbotchatopsdiscord-botllm-agentsllmslocal-llmmcpmodel-context-protocolmulti-agentopenai-compatibleopenclawpersonal-ai-assistantpythonself-hostedtelegram-bot-ai-assistantwebuiworkflow-automation
fix(memory): cap workspace Dream prompt overrides
Xubin Renc9c69e4316e9
diff --git a/nanobot/agent/memory.py b/nanobot/agent/memory.pyindex ecefaec4c..4036c6edf 100644--- a/nanobot/agent/memory.py+++ b/nanobot/agent/memory.py@@ -63,6 +63,7 @@ class MemoryStore:self._corruption_logged = False # rate-limit invalid cursor warningself._malformed_entry_logged = False # rate-limit bad history shape warningself._oversize_logged = False # rate-limit oversized-entry warning+ self._dream_prompt_oversize_logged = Falseself._append_lock = threading.Lock() # serialize cursor allocation + appendself._git = GitStore(workspace, tracked_files=["SOUL.md", "USER.md", "memory/MEMORY.md", "memory/.dream_cursor",@@ -506,7 +507,17 @@ class MemoryStore:with suppress(OSError):text = self.dream_prompt_file.read_text(encoding="utf-8")if text.strip():- return text.rstrip()+ text = text.rstrip()+ if len(text) > _DREAM_PROMPT_MAX_CHARS:+ if not self._dream_prompt_oversize_logged:+ self._dream_prompt_oversize_logged = True+ logger.warning(+ "workspace Dream prompt exceeds {} chars ({}); truncating. "+ "Further occurrences suppressed.",+ _DREAM_PROMPT_MAX_CHARS, len(text),+ )+ return truncate_text(text, _DREAM_PROMPT_MAX_CHARS)+ return textreturn self.default_dream_prompt()def build_dream_prompt(self, *, max_entries: int = 20) -> tuple[str, int] | None:@@ -657,6 +668,7 @@ class MemoryStore:# that catches any new caller that forgot to set its own cap._RAW_ARCHIVE_MAX_CHARS = 16_000 # fallback dump (LLM failed)_ARCHIVE_SUMMARY_MAX_CHARS = 8_000 # LLM-produced consolidation summary+_DREAM_PROMPT_MAX_CHARS = 32_000 # workspace-local Dream prompt override_HISTORY_ENTRY_HARD_CAP = 64_000 # emergency cap in append_historydiff --git a/tests/agent/test_dream.py b/tests/agent/test_dream.pyindex 6427ba1f7..cae82b661 100644--- a/tests/agent/test_dream.py+++ b/tests/agent/test_dream.py@@ -78,6 +78,20 @@ class TestBuildDreamPrompt:assert "## Conversation History" in promptassert "keep this fact" in prompt+ def test_workspace_dream_prompt_override_is_capped(self, store):+ store.dream_prompt_file.parent.mkdir(parents=True)+ store.dream_prompt_file.write_text("x" * 40_000, encoding="utf-8")+ store.append_history("keep this fact")++ result = store.build_dream_prompt()++ assert result is not None+ prompt, _ = result+ assert "x" * 40_000 not in prompt+ assert "... (truncated)" in prompt+ assert "## Conversation History" in prompt+ assert "keep this fact" in prompt+def test_empty_workspace_dream_prompt_uses_default(self, store):store.dream_prompt_file.parent.mkdir(parents=True)store.dream_prompt_file.write_text(" \n", encoding="utf-8")