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
I'll start by exploring the repository structure to understand the codebase and locate the relevant file. Let me first check the current directory.
- Assistant
Let me open the file and examine the
retain_recent_legal_suffixmethod: - Assistant
Let me scroll down to find the
retain_recent_legal_suffixmethod. - Assistant
Let me open the file around line 272 to see the method.
- Assistant
Let me examine the git history to understand the current state and check if there are any existing tests for this function.
- Assistant
Let me check the git log for the specific file to see recent changes.
- Assistant
Let me check the specific commit that seems most relevant to this issue.
- Assistant
Let me check the diff for that commit to understand the changes made.
- Assistant
Let me check the existing tests for this function to understand the expected behavior.
- Assistant
Let me re-read the current
retain_recent_legal_suffixandenforce_file_capmethods carefully. - Assistant
Let me look at the existing tests to understand the expected behavior better.
- Assistant
Let me check all the callers of
retain_recent_legal_suffix. - Assistant
Let me understand the issue more clearly. I'll trace through the bug scenario with a test script.
- Assistant
Now I understand the bug clearly. Let me trace through the issue more precisely with a better test case matching the issue description:
- Assistant
Now I have a clear understanding of the bug. Let me also check the existing tests to see what I need to maintain.
- Assistant
Now I fully understand the bug. Let me trace through the issue:
In the
elsebranch ofretain_recent_legal_suffix, when all recent messages are assistant/tool:retained = self.messages[latest_user : latest_user + max_messages]- starts at the user messagedropped = len(self.messages) - len(retained)- this gives90(not85!)- In
enforce_file_cap,dropped = before[:dropped_count]takes the first90messages, which includes the user message at index 85
The bug is that
enforce_file_capassumes dropped messages are always a prefix of the original list, but in theelsebranch, the retained window starts fromlatest_user(middle of the list), not the end.Let me fix
enforce_file_capto correctly identify the boundary: - Assistant
Now I need to fix
enforce_file_capto correctly identify the dropped messages. The issue is thatbefore[:dropped_count]assumes dropped messages are always from the start, but in theelsebranch, the retained window starts atlatest_user(middle of the list). - Assistant
Now I have a clear understanding. The fix needs to be in both
retain_recent_legal_suffix(to return actual dropped messages) andenforce_file_cap(to use those instead of the incorrectbefore[:dropped_count]). Let me check all callers: - Assistant
Let me create a comprehensive test script to verify the bug and understand the expected behavior:
- Assistant
Now I have a clear understanding of the bug. The problem is in the interaction between
retain_recent_legal_suffixandenforce_file_cap. When the else branch is triggered:retained = self.messages[latest_user : latest_user + max_messages]- starts from the user messagedropped = len(self.messages) - len(retained)- this is20 - 10 = 10- In
enforce_file_cap,before[:10]takes the first 10 messages, which INCLUDESuser_msg9 - But
user_msg9is also inself.messages(retained)
The fix should ensure the actual cut point is tracked correctly. Let me implement the fix:
- Assistant
Now I have a clear understanding of the bug. The issue is in the interaction between
retain_recent_legal_suffixandenforce_file_cap. When the else branch is triggered:retained = self.messages[latest_user:latest_user + max_messages]starts at the user message (e.g., index 85)dropped = len(self.messages) - len(retained) = 90(the count is correct but...)- In
enforce_file_cap,before[:dropped_count]=before[:90]incorrectly includes the user message at index 85
The fix needs to track the actual cut point (where the retained messages start in the original list). Let me implement this:
- Assistant
Now let me run my test to verify the fix works:
- Assistant
The fix works correctly. Now let me run the existing tests to make sure nothing is broken:
- Assistant
All29 existing tests pass. Let me also run any other session-related tests:
- Assistant
All tests pass. Let me also check if there are any callers that might be affected by the return type change (from
Nonetoint): - Assistant
The callers in memory.py and commands.py just call the method and ignore the return value, so the return type change from
Nonetointis backward compatible - they'll just ignore the returned value.Let me verify the callers are not using the return value.
- Assistant
There's the same bug pattern in
memory.pyat line 812! It's computingcut = len(tail) - len(kept)and usingtail[:cut], which would have the same duplication problem. Now thatretain_recent_legal_suffixreturns the correctcut, I need to update this caller to use it. - Assistant
This caller in
memory.pyhas the same bug pattern! It's computingcut = len(tail) - len(kept)which would give the wrong value in the else branch. I need to update it to use the return value: - Assistant
Now let me check the CLI commands caller:
- Assistant
Let me run all the existing tests again to make sure my changes are correct:
- Assistant
All96 tests pass. Let me also check the auto_compact tests since they might exercise the memory.py path:
- Assistant
All tests pass. Let me also run the memory-related tests:
- Assistant
Let me also run the consolidate offset test and loop consolidation tests since those might be affected:
- Assistant
All99 memory-related tests pass. Now let me run a comprehensive test to verify the fix handles edge cases correctly:
- Assistant
All comprehensive tests pass. Let me now verify the final state of my changes:
- Assistant
The diff looks clean and correct. Let me run a final comprehensive test suite to make sure everything works:
- Assistant
All250 tests pass. Let me also run one final edge case test to make sure the fix handles the
find_legal_message_startadjustments correctly: - Assistant
Let me fix that test - when latest_user is at index 0, cut=0 is correct (nothing to drop):
- Assistant
All tests pass. Let me now review the final state of my changes:
- Assistant
Let me run the full test suite one final time:
- Assistant
All250 tests pass. Let me review the final changes one more time:
- Assistant
Let me now look at the full diff once more to make sure everything is correct.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.