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
nanobot Version
0.2.0
Python Version
3.11
Operating System
Windows
Channel / Platform
Weixin (Personal WeChat)
LLM Provider
OpenAI
Configuration (Optional)
Additional 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
The output was truncated. Let me find and look at the
retain_recent_legal_suffixmethodAssistant
me look at the
enforce_file_capto understand the calling pattern:Assistant
Let me see the full call graph for
retain_recent_legal_suffixand understand more about how it's used:Assistant
me look at the other callers to understand the full context:
Assistant
Let me search for
_RECENT_SUFFIX_MESSAGES:Assistant
Now let me look at the full manager.py once more to understand the relevant section fully:
Assistant
Now let me write a reproduction script to confirm the bug:
Assistant
confirmed. Now let me apply the fix:
Assistant
Now let me implement the fix:
Assistant
Let me try using
replaceinstead:Assistant
Let me verify the change:
Assistant
Now let me update the
enforce_file_capmethod to use the new return value:Assistant
Now let me update the callers in
memory.pyand the test:Assistant
Now let me also fix the test file:
Assistant
me check how the test calls this fake function:
Assistant
Let me look at the fake test more carefully - it actually uses the real
retain_recent_legal_suffixon the probe:Assistant
run the existing tests to see if my fix breaks anything:
Assistant
All session manager tests pass. Let me also test the auto compact tests:
Assistant
Now let me verify my fix using my reproduction script:
Assistant
reproduction script uses the old buggy pattern. Let me update it to test the new fixed behavior:
Assistant
All tests pass. Let me also check what
enforce_file_capreturns now via my reproduction script - I'll re-run the original script that simulatedenforce_file_cap:Assistant
The fix works correctly. Now let me run all the related tests to make sure I haven't broken anything:
Assistant
All 1052 tests pass. Let me also run the entire test suite to make sure no other tests break:
Assistant
Let me check if the failing test is related to my changes:
Assistant
test is unrelated to my changes - it's about MCP presets. Let me verify by checking if the test was failing before my changes:
Assistant
The test was already failing before my changes, so it's a pre-existing failure unrelated to this issue. My changes are correctly applied.
Let me also verify the changes I made:
Assistant
me check the
find_legal_message_startfunction to understand its behavior:Assistant
Let me write a more thorough edge case test:
Assistant
All 14 edge cases pass. Let me also verify the tests in the repo that test
retain_recent_legal_suffixstill pass:Assistant
All 114 related tests pass. Let me also clean up my temp files:
Assistant
Let me check the git status to see what I've changed:
Assistant
do a final comprehensive test run to confirm everything works:
Assistant
All 1211 tests pass in the agent, session, and cli test directories. Let me also run a final reproduction test to confirm the bug is fixed:
Assistant
Let me debug this:
Assistant
Oh I see - the test has a bug. I'm comparing references from different
make_messagescalls. Let me fix the test:Assistant
The bug is fully fixed. Let me clean up and verify the final state:
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.