ISSUE DESCRIPTION Bug: retainrecentlegalsuffix 在 else 分支下会导致用户消息被重复归档 Bug Description 在…
- User
ISSUE DESCRIPTION
Bug: retain_recent_legal_suffix 在 else 分支下会导致用户消息被重复归档
Bug Description
在
nanobot/session/manager.py的retain_recent_legal_suffix方法中,当 else 分支(最近 N 条消息全是助手/工具消息)被触发时,由于cut值的计算使用len(tail) - len(kept),而没有考虑用户消息的实际位置,导致用户消息被错误地同时归档到 archive 和 kept 中。具体影响:
- 用户消息同时出现在 archive(待压缩)和 kept(保留)中
- 可能导致 LLM 上下文不一致
Steps to Reproduce
- 会话中消息数量超过 _RECENT_SUFFIX_MESSAGES (默认10条)
- 最近 N 条消息中 不包含任何用户消息 (全是 assistant/tool 角色)
- 在完整会话历史中,最后一条用户消息位于倒数第 11-15 条之间
Expected Behavior
当会话历史中最近 N 条消息全是助手/工具消息,而用户消息位于更早位置时:
- 用户消息不应被重复归档 - 用户消息应该只保留在 kept 中,不应该同时出现在 archive 和 kept 中
- 归档边界应该正确 - cut 值应该基于用户消息的实际位置,而不是简单的 len(tail) - len(kept)
- 上下文连贯性应该保持 - 从用户消息开始的完整上下文应该被保留,不应该被错误切割
假设:
tail = messages[0:100](100条)
latest_user = 85(用户消息位置)
max_messages = 10
期望:
cut = latest_user # 应该是 85,不是 90 archive = tail[:85] # [0-84],不包含用户消息 kept = tail[85:] # [85-99],包含用户消息及其后续内容
结果:
archive 和 kept 没有重叠 ✅
用户消息只在 kept 中 ✅
上下文连贯 ✅
总结
期望行为 :无论用户消息位于何处,它都应该:
- 只出现在 kept 中(保留消息)
- 不出现在 archive 中(归档消息)
- 作为保留消息的起始点,确保上下文连贯
Relevant Logs
nanobot Version
0.2.0
Python Version
3.11
Operating System
Windows
Channel / Platform
Weixin (Personal WeChat)
LLM Provider
OpenAI
Configuration (Optional)
Additional Context
No response
Interface
Type: Function Name: retain_recent_legal_suffix Path: nanobot/session/manager.py Input: self, max_messages: int Output: tuple[list[dict], int] Description: Trims the session's message list to at most
max_messageswhile preserving a legal suffix (ending with a user message if possible). Returns the list of dropped messages and a count of how many of those were already in the consolidated prefix, so callers can archive only new, unconsolidated messages.ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use to restore the file to its original state. You can also reset all changes in the repository using command. Additionally, you can use and to temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you call ) using or commands. However you don't need to use or before submitting your changes. In fact, only submits currently unstaged changes because it uses to get the changes that need to be submitted.
- Assistant
I'll start by exploring the repository structure to understand the codebase and locate the problematic function. Let me first examine the directory.
- Assistant
Now let's open the
nanobot/session/manager.pyfile to understand theretain_recent_legal_suffixmethod. - Assistant
Let me search for the
retain_recent_legal_suffixmethod in the file. - Assistant
Now let me open the file at line 272 to see the method definition.
- Assistant
Let me scroll down to see the entire
retain_recent_legal_suffixmethod and theenforce_file_capmethod. - Assistant
- Assistant
Now I understand the bug clearly. Let me look at the
get_historymethod andfind_legal_message_startto understand the full context: - Assistant
Let me check the
find_legal_message_startfunction to understand what it does. - Assistant
Let me first create a test script to reproduce the bug:
- Assistant
Great! I've successfully reproduced the bug. The user message at position 85 is both in
droppedand inretained. Now let me understand the issue better. The problem is inenforce_file_capwheredropped = before[:dropped_count]assumes a simple prefix-suffix split, butretain_recent_legal_suffixmay produce a retained set that starts from the middle of the original list.Let me examine the expected behavior more carefully:
- Assistant
Let me look at the
get_historymethod to understand how the session is used: - Assistant
Now I understand the full picture. The issue is in
enforce_file_capwheredropped = before[:dropped_count]assumes a simple prefix-suffix split, butretain_recent_legal_suffixmay produce a retained set that starts from the middle of the original list. Let me create a fix: - Assistant
Now I'll implement the fix. The issue is in
enforce_file_capwheredropped = before[:dropped_count]assumes the retained messages are always a suffix. But in theelsebranch ofretain_recent_legal_suffix, the retained messages start from the middle (atlatest_user). The fix should compute the correct split point by finding where the retained messages start in the original list. - Assistant
Now let me test the fix:
- Assistant
Great! The fix works. Now let me test edge cases:
- Assistant
Let me test another edge case where
find_legal_message_startmight trim messages: - Assistant
Good, the fix handles edge cases correctly. Now let me run existing tests to make sure I didn't break anything:
- Assistant
Let me run the tests related to session management:
- Assistant
Let me also run the agent tests that might be related:
- Assistant
Let me run a broader set of tests to make sure nothing else is broken:
- Assistant
Let me check if there are any specific tests for
enforce_file_cap: - Assistant
Let me check those specific tests:
- Assistant
Let me run those specific tests:
- Assistant
Let me also run the test for
retain_recent_legal_suffix: - Assistant
Let me run a comprehensive test to verify my fix handles all edge cases:
- Assistant
Let me run the full test suite to make sure nothing else is broken:
- Assistant
Let me check if that failing test is related to my change:
- Assistant
Let me verify this test failure is unrelated to my change by checking if it was already failing before:
- Assistant
Good, the test failure is pre-existing and not related to my change. Let me run all tests except that one:
- Assistant
Let me review my changes to make sure they're correct:
- Assistant
Let me check if there are any other places where
dropped_countis used: - Assistant
Let me also verify that my fix handles the case where
self.messagesis empty afterretain_recent_legal_suffix: - Assistant
Let me test one more edge case:
- Assistant
Let me clean up the temporary test files:
- Assistant
Let me check the final state of the file to make sure everything is correct:
- Assistant
Let me run one final verification test:
- Assistant
Perfect! The fix is working correctly. Let me clean up the temporary test file:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.