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
Now let's write a reproduction script to verify the fix resolves the duplication bug (via
enforce_file_cap), and then run existing tests.Assistant
10 kept + 90 archived = 100 total, matches exactly. Now let's confirm before the fix this same script showed duplication and/or data loss (to validate the reproduction captured the bug). Let's check with git stash.
Assistant
Confirmed the bug reproduces before the fix (kept overlapping with archived, plus
tail9..tail13silently lost entirely). Now the fix resolves both issues. Let's run the full existing test suite related to session manager and auto-compact.Assistant
Let me revert this and implement the more robust identity-based dropped-tracking fix instead.
Assistant
Now let's update
memory.py'scompact_idle_sessionto use the new return value correctly.Assistant
Now let's check other callers to confirm they don't rely on the old
Nonereturn, and re-run the full test suite.Assistant
That call in
commands.pyjust ignores the return value, which is fine. Now let's run the reproduction script and the test suite.Assistant
All pass. Let's write a targeted reproduction for the
compact_idle_sessionpath too (memory.py), matching the issue's exact scenario, and run the full test suite for good measure.Assistant
Let's find existing tests exercising this exact function to confirm they still pass, and check its test file for scenarios similar to the issue.
Assistant
These all passed already (part of test_consolidator.py run above). Let's run the entire test suite to be safe.
Assistant
Confirmed pre-existing failure unrelated to my change. Now let's clean up the temp script and review the final diff.
Assistant
All existing calls just ignore the return value (no unpacking), so the new tuple return is backward compatible. Let's view the final file section to make sure everything reads well.
Assistant
This looks solid. Let's run the reproduction scenario once more as a final sanity check, plus the full previously-affected test files.
Assistant
Let's run the full test suite once more to make sure everything is stable.
Assistant
All 3775 tests pass (only the pre-existing unrelated failure deselected). Let's do a final review of the diff.
Assistant
Now let me go through the checklist explicitly:
- Reproduced the reported error: Yes — I built a script simulating the described scenario (recent tail with no user message, latest user message further back than the retained window's forward-capped slice) and confirmed with the original code that the user message (and other retained messages) appeared in both the archived chunk and the kept messages, and that some recent messages were silently lost entirely.
- Confirmed the fix resolves it: Yes — after the fix, running the same reproduction shows zero overlap between kept and archived messages, and the total message count is fully accounted for (no data loss).
- Edge cases considered:
max_messages <= 0(clear-session path),len(messages) <= max_messages(no-op path), thefirst_userbranch (already suffix-safe, untouched), and thelast_consolidatedbookkeeping generalization (verified against the existing test that checks a nonzero leftoverlast_consolidated). The new identity-based diff works regardless of whether the dropped messages form a contiguous prefix or a prefix+trailing-remainder combination.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.