refactor(context): trim replay cap plumbing

This commit is contained in:
chengyongru
2026-06-29 14:23:55 +08:00
committed by Xubin Ren
parent 40282e3b74
commit 57f0c859fc
5 changed files with 29 additions and 51 deletions
+3 -4
View File
@@ -29,7 +29,6 @@ from nanobot.utils.subagent_channel_display import scrub_subagent_announce_body
FILE_MAX_MESSAGES = 2000
MIN_REPLAY_MAX_MESSAGES = 120
REPLAY_TOKENS_PER_MESSAGE = 100
DEFAULT_REPLAY_MAX_MESSAGES = FILE_MAX_MESSAGES
_MESSAGE_TIME_PREFIX_RE = re.compile(r"^\[Message Time: [^\]]+\]\n?")
_LOCAL_IMAGE_BREADCRUMB_RE = re.compile(r"^\[image: (?:/|~)[^\]]+\]\s*$")
_TOOL_CALL_ECHO_RE = re.compile(r'^\s*(?:generate_image|message)\([^)]*\)\s*$')
@@ -48,7 +47,7 @@ _FORK_VOLATILE_METADATA_KEYS = {
def replay_max_messages_for_context(context_window_tokens: int | None) -> int:
if not context_window_tokens or context_window_tokens <= 0:
return DEFAULT_REPLAY_MAX_MESSAGES
return FILE_MAX_MESSAGES
return min(
FILE_MAX_MESSAGES,
max(MIN_REPLAY_MAX_MESSAGES, context_window_tokens // REPLAY_TOKENS_PER_MESSAGE),
@@ -144,7 +143,7 @@ class Session:
def get_history(
self,
max_messages: int = DEFAULT_REPLAY_MAX_MESSAGES,
max_messages: int = FILE_MAX_MESSAGES,
*,
max_tokens: int = 0,
extend_to_user: bool = False,
@@ -155,7 +154,7 @@ class Session:
token budget from the tail (``max_tokens``) when provided.
"""
unconsolidated = self.messages[self.last_consolidated:]
max_messages = max_messages if max_messages > 0 else DEFAULT_REPLAY_MAX_MESSAGES
max_messages = max_messages if max_messages > 0 else FILE_MAX_MESSAGES
start_idx = recent_message_start_index(
unconsolidated,
max_messages,