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 what we're working with.
Assistant
Let me explore the repository structure further to find the relevant file.
Assistant
Let me find the session manager file.
Assistant
Let me open the manager.py file to find the
retain_recent_legal_suffixmethod.Let me open the manager.py file to find theretain_recent_legal_suffixmethod.Let me open the manager.py file.I'll open the manager.py file to find theretain_recent_legal_suffixmethod.Assistant
Let me search specifically for the
retain_recent_legal_suffixmethod.Assistant
Let me look at the
retain_recent_legal_suffixmethod in detail.Assistant
Now let me look at the
enforce_file_capmethod to understand the broader context.Assistant
Let me look at the test files to see how this is tested.
Assistant
Let me look at the session tests.
Assistant
Let me check if there are tests for the
enforce_file_caporretain_recent_legal_suffixmethod.Assistant
Let me look at the relevant tests to understand the expected behavior.
Assistant
Let me look at the hard_cap test and more relevant tests.
Assistant
Let me also look at the test_auto_compact.py to understand the expected behavior of
enforce_file_cap.Let me also look at the test_auto_compact.py to understand the expected behavior ofenforce_file_cap.</mm:think>Let me also look at the test_auto_compact.py to understand the expected behavior ofenforce_file_cap.Assistant
Let me look at the rest of the
_fake_compacttest to understand the broader context.Assistant
Let me look at the rest of the test file.
Assistant
Let me look at
find_legal_message_startto understand the hard cap logic.Assistant
Let me look at the function.
Assistant
Now let me write a reproducer to confirm the bug first.
Assistant
Bug confirmed! Now let me run the existing tests to make sure they pass.
Assistant
Let me see the rest of the test file output to make sure all tests pass.
Assistant
Let me also look at the helpers in the test file.
Assistant
Let me check if there are any other places in the codebase that call
retain_recent_legal_suffix.Assistant
Let me look at the other usages of
retain_recent_legal_suffixto ensure my fix doesn't break them.Assistant
Let me see the broader context of this function.
Assistant
Let me see the cli/commands.py usage.
Assistant
Now I have a clear understanding of the bug and the fix. Let me apply the fix to
retain_recent_legal_suffixandenforce_file_cap.Assistant
Now let me apply the fix. I'll modify
retain_recent_legal_suffixto track the cut accurately and return the dropped messages.Assistant
Now let me update
enforce_file_capto use the new return value.Assistant
Now let me replace the
enforce_file_capmethod.Assistant
Now let me also fix
compact_idle_sessioninnanobot/agent/memory.pysince it has the same bug pattern.Assistant
Now let me update
compact_idle_sessionto use the new return value.Assistant
Now let me verify my fix by running the reproducer.
Assistant
The fix is working correctly! The user message is no longer duplicated. The count "mismatch" is actually correct behavior - the 5 messages after
latest_user + max_messagesare intentionally dropped due to the hard cap. Let me update the verification script to reflect this.Assistant
The fix works! Now let me also verify with
enforce_file_cap:Assistant
Both
retain_recent_legal_suffixandenforce_file_capare fixed. Now let me run the existing tests to make sure I haven't broken anything.Assistant
All 79 tests pass. Let me also run a broader test to make sure I haven't broken anything else.
Assistant
Let me check if the failing test is related to my changes.
Assistant
Let me verify this test was already failing before my changes.
Assistant
Good - the test was already failing before my changes. It's not related to my fix.
Now let me think about edge cases and write a comprehensive test to verify them.
Assistant
The test had a wrong expectation. Let me fix the test - the expected behavior is to keep positions 0-4, with 0 dropped. This is the correct behavior because the user is at position 0 (very beginning), so we anchor to it.
Assistant
My test was wrong - the user at position 15 is within the last 5 messages, so the first branch is used. Let me fix the test to actually trigger the else branch.
Assistant
All edge case tests pass. Let me also re-run all the relevant tests to make sure my fix doesn't break them.
Assistant
All 1077 tests pass. Let me also check what the
compact_idle_sessioninnanobot/agent/memory.pylooks like now.Assistant
Let me write a test for
compact_idle_sessionto verify the fix.Assistant
Let me find the actual class name.
Assistant
The class is
Consolidator. Let me check the test for it.Assistant
Let me check the Consolidator class structure.
Assistant
Let me see if there are tests for
compact_idle_sessionthat I might be missing.Assistant
Let me check the test at line 432 to see if it tests the offset case.
Assistant
Let me run the test for
compact_idle_sessionto make sure my fix works.Assistant
All 28 tests for
test_consolidator.pypass. Let me also run the complete test suite to make sure nothing is broken.Assistant
Only the same pre-existing test failure (unrelated to my changes). Let me do a final review of my changes.
Assistant
Let me also look at the memory.py diff.
Assistant
Let me run all the relevant test files one more time to confirm.
Assistant
All 142 tests pass. Let me run a final comprehensive test using my reproducer.
Assistant
Let me clean up temporary files.
Assistant
Let me verify the final state of the code.
Assistant
Let me do a final comprehensive test that demonstrates the original bug, then verifies the fix.
Assistant
All tests pass. Let me clean up the temp file.
Assistant
Let me do one more comprehensive review of my changes.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.