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
shellnanobot Version
0.2.0
Python Version
3.11
Operating System
Windows
Channel / Platform
Weixin (Personal WeChat)
LLM Provider
OpenAI
Configuration (Optional)
yamlAdditional 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
Let me start by understanding the repository structure and the specific file mentioned in the issue.
Assistant
Let me look at the specific file mentioned in the issue:
nanobot/session/manager.py.Assistant
Let me find the
retain_recent_legal_suffixAssistant
Let me look at the method and its surrounding context.
Assistant
Let me look at the full
enforce_file_capmethod and howretain_recent_legal_suffixis called.Assistant
Let me check the git history of this file and look for how this method evolved
Assistant
Let me check the diff of the relevant commit to understand the evolution:
Assistant
Now I have a clear understanding of the bug. Let me trace through the code to confirm
Assistant
Bug confirmed. Messages 85-89 appear in both dropped and retained sets. Now let me test my fix approach
Assistant
Let me see how
retain_recent_legal_suffixis called elsewhereAssistant
Let me look at the relevant tests
Assistant
Let me also check the hard cap test:
Assistant
Let me check
find_legal_message_start:Assistant
Let me look at the function.
Assistant
Now I have a thorough understanding of the issue. Let me verify my understanding and then make the fix.
The bug: In the
elsebranch ofretain_recent_legal_suffix, the code creates a forward windowself.messages[latest_user: latest_user + max_messages]that is NOT a suffix ofself.messages. Whenenforce_file_capcalculatesdropped = before[:dropped_count], it assumes the kept messages form a suffix, leading to messages being in botharchive(dropped) andkept(retained).The fix: Change the else branch to produce a suffix
self.messages[latest_user:]. The existing hard-cap code will then correctly truncate from the front.Let me make the edit:
Assistant
Now I need to change line 293. Let me see the exact content:
Assistant
Now let me make the fix:
Assistant
Now let me update the comment to reflect the change
Assistant
Now let me run the existing tests to make sure they still pass:
Assistant
Let me check what
_tool_turnproduces:Assistant
Let me look at the `_tool_turn
Assistant
I see - the test fails because my fix changes behavior when the suffix is too long. The hard-cap truncation loses the user message. I need a different approach. Let me revert my change and instead fix the
enforce_file_capmethod which is where the duplication actually manifests (when computingdropped = before[:dropped_count]Assistant
Good, reverted. Now let me look at
enforce_file_capand fix it properly. The issue is at line 331:dropped = before[:dropped_count]assumes retained is a suffix, but when the else branch triggers inretain_recent_legal_suffix, retained may not be a suffixAssistant
Now I need to fix
enforce_file_cap. The problem isdropped = before[:dropped_count]at line 331 which assumes retained is a suffix. I'll replace it with code that finds the actual cut point by locating the first kept message inbeforeAssistant
Now let me verify the fix works correctly with a test script:
Assistant
The fix works. No duplication, user message properly preserved in kept. Now let me run all the relevant
Assistant
All 29 tests pass. Let me also run the auto_compact tests and any other related tests:
Assistant
All tests pass. Let me also run the agent tests broadly to make sure nothing breaks
Assistant
Let me run the agent tests without timeout
Assistant
All 1052 tests pass. Let me verify my changes are clean and correct
Assistant
Let me run one final comprehensive test to validate the fix handles all edge cases:
Assistant
All checks pass. Let me now go through the checklist:
Checklist Review
-
If an error was reported in the issue description, I have successfully reproduced it. ✅ Yes. I created a simulation script that demonstrated 5 messages appearing in both the "dropped" (archived) and "kept" (retained) sets when the else branch triggers in
retain_recent_legal_suffix. -
If an error was reported in the issue description, I have confirmed that my fix resolves the error. ✅ Yes. After the fix, the duplication count is 0, the user message at position 85 is correctly in the kept messages (not archived), and the cut point is correctly at index 85.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.