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 exploring the repository structure to understand the codebase
Assistant
Let me look at the existing tests for this functionality:
Assistant
Looking at lines 79-82 in the test:
pythonprobe.retain_recent_legal_suffix(max_suffix) kept = probe.messages cut = len(tail) - len(kept) archive_msgs = tail[:cut]This is exactly the buggy pattern!
cut = len(tail) - len(kept)without considering the actual position of the user message.Now let me check the existing tests for enforce_file_cap:
Assistant
Now let me look at the calling code in
loop.pyandmemory.py:Assistant
me check
find_legal_message_start:Assistant
me look at all the call sites to understand the impact of changing the return type:
Assistant
me look at the test callers in test_session_manager_history.py more carefully:
Assistant
let me create a reproduction script to confirm the bug:
Assistant
confirmed. Now let me also check
enforce_file_caphas the same issue:Assistant
Now let me look at the broader codebase to check for any other call sites I might have missed:
Assistant
Now I have a complete understanding. Let me implement the fix. The core change is to make
retain_recent_legal_suffixproperly track and return the actual cut point (which messages were dropped from the front), and then update its callers to use that returned value instead of computing it incorrectly vialen(tail) - len(kept).Assistant
Now let me apply the fix:
Assistant
I need to actually invoke the edit command now.
Assistant
Let me actually call the edit command
Assistant
Now let me update
enforce_file_capto use the new return value:Assistant
Now let me update the
compact_idle_sessioninmemory.pyto use the new return value:Assistant
Now let me run my reproduction scripts to verify the fix:
Assistant
reproduction script was using the old buggy code pattern. Let me update it to use the new fixed pattern:
Assistant
The overlap detection is using string contents as set keys, but the same content strings appear multiple times. Let me fix the verification to use identity checks:
Assistant
All three code paths are now fixed. Let me run the existing tests to make sure I haven't broken anything:
Assistant
1052 tests in
tests/agent/pass. Let me run more tests to be thorough:Assistant
One test failed but it looks unrelated. Let me check:
Assistant
Good - this test was already failing before my changes. It's unrelated to the bug I'm fixing. Let me restore my changes:Good - this test was failing BEFORE my changes, so it's unrelated. Let me restore my changes:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[git stash pop]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Let me actually invoke the tool
Assistant
Good, my changes are intact. Now let me run the rest of the tests, skipping the unrelated failing test:
Assistant
All 3759 tests pass. Let me also verify edge cases:
Assistant
All edge cases pass. Let me also add a test for the bug scenario in a temp script and verify it's caught properly:
Assistant
Let me also test a scenario with tool calls to verify the legal message start adjustment works correctly with the cut tracking:
Assistant
me also test a complex scenario combining tools and the bug pattern:
Assistant
fix works correctly - the kept messages start at the latest user. The error was just my verification code's mistake. Let me fix the test:
Assistant
The fix works correctly for complex scenarios too. Let me also verify the final state of the fix:
Assistant
Let me also look at the tests file for
_make_fake_compactto see if it should also be updated (the test file has the same buggy pattern):Assistant
me verify my fix matches the expected behavior from the issue:
Assistant
The issue is resolved. Let me also clean up the temporary files and run a final check:
Assistant
Let me run the relevant tests one final time to confirm everything still passes:
Assistant
All 142 relevant tests pass. Let me also do a final sanity check by reviewing my changes:
Assistant
The diff looks clean. Let me now go through the checklist:
Checklist:
-
If an error was reported in the issue description, I have successfully reproduced it. ✅
- I created reproduction scripts that confirmed the bug in both
compact_idle_session(via the fake_make_fake_compactpattern) and .
- I created reproduction scripts that confirmed the bug in both
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.