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 issue. The bug is in
nanobot/session/manager.pyin theretain_recent_legal_suffixmethod. The problem is that when the else branch is triggered (recent N messages are all assistant/tool messages), thecutvalue calculation is wrong, causing user messages to be duplicated in both archive and kept.Let me first explore the repository structure and then look at the specific file.
Assistant
Let's look at the specific file mentioned in the issue.
Assistant
The file was truncated. Let me find the
retain_recent_legal_suffixmethod specificallyAssistant
Let me read the method starting at line 272
Assistant
Let me see the full
enforce_file_capmethod and surrounding codeAssistant
Now I understand the bug. Let me also check the
_RECENT_SUFFIX_MESSAGESconstant and see if there are tests for this method:Assistant
Let me run just one command at a time.
Assistant
Let me look at the tests for
retain_recent_legal_suffix:Assistant
Let me look at the hard cap test and the test for the specific scenario around line 523
Assistant
Let me look at
find_legal_message_startto understand what it does:Assistant
Let me look at
find_legal_message_startand the test helper `_tool_tAssistant
Let me run one command at a time.
Assistant
Now let me create a reproduce script to confirm the bug:
Assistant
Bug confirmed. Now let me implement the fix. The issue is that
droppedis computed aslen(self.messages) - len(retained), but in the else branch,retainedstarts atlatest_userposition rather than at the end. I need to track the actual cut position.Let me open the file and apply
Assistant
Now let me apply the fix. I'll replace the method with the correct implementation that tracks the actual
Assistant
Now let me verify the fix by running the reproduce script again
Assistant
The script still shows the bug because
enforce_file_capalso computesdropped_countincorrectly. But wait, my reproduce script doesn't useenforce_file_cap—it directly callsretain_recent_legal_suffixand then manually computesdroppedthe same flawed way. Let me update the reproduce script to check the actual internal state.But first, the real issue: I need to check if
enforce_file_caphas the same problem, and also verify my fix toretain_recent_legal_suffixis working correctly internally. Let me update the reproduce scriptAssistant
Good, the fix in
retain_recent_legal_suffixis working. Now I need to also fixenforce_file_capwhich computesdropped_countthe same flawed way. Let me check all callersAssistant
Let me look at the other callers to understand the full context
Assistant
Let me just run one command.
Assistant
Let me also check the cli caller
Assistant
Now I see the full picture. There are TWO places that compute the cut incorrectly:
enforce_file_capinmanager.pymemory.pyline 812-813
The cleanest fix is to have
retain_recent_legal_suffixreturn the correctdroppedcount and update both callers. Let me makeAssistant
Let me check the current state of the file after that
Assistant
Let me see the full method as it currently stands
Assistant
I see the issue - the
dropped = cutline was removed. Let me fix this with a proper edit:Assistant
I see a duplicate line. Let me remove it and update the signature
Assistant
Good, the duplicate is removed. Now update the function signature to reflect return type
Assistant
Let me use replace for the signature
Assistant
Let me navigate to line 272 first
Assistant
Now let me replace the signature
Assistant
Let me use the heredoc syntax for the replace command
Assistant
Now I need to fix the early return statements and
enforce_file_cap. Let me fix the early returns firstAssistant
Now fix
enforce_file_capto use the returnedAssistant
Now let me clean up the unused
before_countvariableAssistant
Now fix the
memory.pycaller. Let me look at the fullAssistant
Now fix the `memory.py
Assistant
Now let me run the reproduce script again to verify
Assistant
The script computes
dropped_countthe same flawed way. Let me update it to verify using the returned valueAssistant
All tests pass. Now let me run the existing tests to make sure I haven't
Assistant
All 29 tests pass. Let me also run the auto_compact tests
Assistant
All 50 tests pass. Let me also run the autocompact unit
Assistant
All tests pass. Let me do a final syntax check on both modified
Assistant
Let me verify the final state of the changes
Assistant
The diff looks clean and correct. Let me now run one final comprehensive check - a quick edge case test to make sure other scenarios still work:
Assistant
All edge cases pass. Let me clean up temporary
Assistant
The fix is complete. Let me summarize what was done:
Summary
Bug
In
retain_recent_legal_suffix, when the else branch triggered (recent N messages are all assistant/tool, with the last user message further back), thedroppedcount was computed aslen(self.messages) - len(retained). This assumedretainedalways starts at the end of the message list, but in the else branch,retainedstarts atlatest_userposition. This caused messages betweenlatest_userandn - max_messagesto appear in BOTH the archive (dropped) and kept sets.Changes Made
-
nanobot/session/manager.py-retain_recent_legal_suffix:- Now tracks
cut(the actual cut position) correctly for all branches - Changed return type from to , returning the correct count
- Now tracks
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.