fix(memory): cap workspace Dream prompt overrides

This commit is contained in:
Xubin Ren
2026-07-03 00:41:51 +08:00
parent 8a79eb1aaa
commit c9c69e4316
2 changed files with 27 additions and 1 deletions
+13 -1
View File
@@ -63,6 +63,7 @@ class MemoryStore:
self._corruption_logged = False # rate-limit invalid cursor warning self._corruption_logged = False # rate-limit invalid cursor warning
self._malformed_entry_logged = False # rate-limit bad history shape warning self._malformed_entry_logged = False # rate-limit bad history shape warning
self._oversize_logged = False # rate-limit oversized-entry warning self._oversize_logged = False # rate-limit oversized-entry warning
self._dream_prompt_oversize_logged = False
self._append_lock = threading.Lock() # serialize cursor allocation + append self._append_lock = threading.Lock() # serialize cursor allocation + append
self._git = GitStore(workspace, tracked_files=[ self._git = GitStore(workspace, tracked_files=[
"SOUL.md", "USER.md", "memory/MEMORY.md", "memory/.dream_cursor", "SOUL.md", "USER.md", "memory/MEMORY.md", "memory/.dream_cursor",
@@ -506,7 +507,17 @@ class MemoryStore:
with suppress(OSError): with suppress(OSError):
text = self.dream_prompt_file.read_text(encoding="utf-8") text = self.dream_prompt_file.read_text(encoding="utf-8")
if text.strip(): 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 text
return self.default_dream_prompt() return self.default_dream_prompt()
def build_dream_prompt(self, *, max_entries: int = 20) -> tuple[str, int] | None: 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. # that catches any new caller that forgot to set its own cap.
_RAW_ARCHIVE_MAX_CHARS = 16_000 # fallback dump (LLM failed) _RAW_ARCHIVE_MAX_CHARS = 16_000 # fallback dump (LLM failed)
_ARCHIVE_SUMMARY_MAX_CHARS = 8_000 # LLM-produced consolidation summary _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_history _HISTORY_ENTRY_HARD_CAP = 64_000 # emergency cap in append_history
+14
View File
@@ -78,6 +78,20 @@ class TestBuildDreamPrompt:
assert "## Conversation History" in prompt assert "## Conversation History" in prompt
assert "keep this fact" in prompt assert "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): def test_empty_workspace_dream_prompt_uses_default(self, store):
store.dream_prompt_file.parent.mkdir(parents=True) store.dream_prompt_file.parent.mkdir(parents=True)
store.dream_prompt_file.write_text(" \n", encoding="utf-8") store.dream_prompt_file.write_text(" \n", encoding="utf-8")