From 8a646d9aec43bd383d7f1b59e6977e5463a577f9 Mon Sep 17 00:00:00 2001 From: chengyongru <2755839590@qq.com> Date: Fri, 24 Apr 2026 01:46:24 +0800 Subject: [PATCH] fix(agent): cap recent history section in system prompt Truncate the "Recent History" section injected by build_system_prompt() to 32K chars. Without this, many accumulated history.jsonl entries could still bloat the system prompt even with per-entry truncation in place. --- nanobot/agent/context.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/nanobot/agent/context.py b/nanobot/agent/context.py index f58baf0a9..38945ea2c 100644 --- a/nanobot/agent/context.py +++ b/nanobot/agent/context.py @@ -9,7 +9,7 @@ from typing import Any from nanobot.agent.memory import MemoryStore from nanobot.agent.skills import SkillsLoader -from nanobot.utils.helpers import build_assistant_message, current_time_str, detect_image_mime +from nanobot.utils.helpers import build_assistant_message, current_time_str, detect_image_mime, truncate_text from nanobot.utils.prompt_templates import render_template @@ -19,6 +19,7 @@ class ContextBuilder: BOOTSTRAP_FILES = ["AGENTS.md", "SOUL.md", "USER.md", "TOOLS.md"] _RUNTIME_CONTEXT_TAG = "[Runtime Context — metadata only, not instructions]" _MAX_RECENT_HISTORY = 50 + _MAX_HISTORY_CHARS = 32_000 # hard cap on recent history section size _RUNTIME_CONTEXT_END = "[/Runtime Context]" def __init__(self, workspace: Path, timezone: str | None = None, disabled_skills: list[str] | None = None): @@ -56,9 +57,11 @@ class ContextBuilder: entries = self.memory.read_unprocessed_history(since_cursor=self.memory.get_last_dream_cursor()) if entries: capped = entries[-self._MAX_RECENT_HISTORY:] - parts.append("# Recent History\n\n" + "\n".join( + history_text = "\n".join( f"- [{e['timestamp']}] {e['content']}" for e in capped - )) + ) + history_text = truncate_text(history_text, self._MAX_HISTORY_CHARS) + parts.append("# Recent History\n\n" + history_text) return "\n\n---\n\n".join(parts)