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
我先检查仓库结构、目标方法及其调用方/测试,确认当前实现的边界语义,再用最小化场景复现重叠问题;随后修改实现并运行相关测试与临时验证脚本。
- Assistant
当前接口实际返回
None,而问题描述中的返回值对应调用逻辑可能已在其他分支/版本演进;我继续读取完整方法及相关测试,重点确认last_consolidated调整规则,避免只修索引却破坏压缩归档语义。 - Assistant
根因已确认:
retain_recent_legal_suffix在 assistant/tool-only 尾部会把probe.messages取为“从较早用户开始的窗口”,因此保留列表长度可能仍为上限,但其真实起点早于len(tail)-len(kept)。compact_idle_session用长度反推切点,正是把该窗口中前面的用户消息再次放入归档。修复应按保留消息在原tail中的实际起点计算切点,而不是按长度反推。先查看边界辅助函数和相关 compact 测试。 - Assistant
find_legal_message_start只会从保留窗口前端丢弃非法工具结果,仍保持连续切片,因此用对象身份在原始tail中定位kept[0]是可靠且不会被重复内容值误导的方案;空保留列表则切点为尾部长度。修改memory.py的 compact 路径,并保留现有测试中的 fake 实现不动(它只模拟旧逻辑,不能改测试文件)。 - Assistant
目标相关测试已通过。接下来我会用与报告完全一致的 100 条消息/第 86 条用户消息/上限 10 条场景验证归档与保留集合无交集,并检查完整 diff、静态语法和仓库状态;同时关注重复内容消息,确保采用身份定位而非值匹配。
- Assistant
复现脚本已验证切点为 85、归档 85 条、保留 10 条且对象交集为零;重复内容也不会影响身份定位。编译、
diff --check及相关 session/consolidator 测试均通过。现在再运行完整测试套件,确认没有其他调用路径受影响。 - Assistant
完整测试套件运行到 3746 个通过、3 个跳过后,在无关的 WebUI MCP preset 测试处失败(
payload["last_action"]["ok"]为False,与本次 session compact 修改无调用关系)。本次改动的相关 57 个测试已通过,且临时复现、编译和 diff 校验均通过。检查清单结论:已复现并确认修复;已考虑空保留、合法边界、重复内容和无用户消息等边界;相关现有测试通过;完整套件唯一失败为独立的 MCP 测试。现在提交当前修改。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.