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
我先检查目标实现、调用方和相关测试,确认
cut、consolidated prefix 计数及消息角色约束之间的关系,再构造最小复现。执行:shellsed -n '1,260p' nanobot/session/manager.py && printf '\n--- references/tests ---\n' && grep -R "retain_recent_legal_suffix" -n . --exclude-dir=.git - Assistant
目标方法的实际注解是
-> None,与 issue 的接口描述略有差异;需要查看完整逻辑以及测试对“硬上限”和last_consolidated的既有要求,避免只改索引却破坏计数语义。执行:shellsed -n '240,360p' nanobot/session/manager.py sed -n '110,195p' tests/agent/test_session_manager_history.py sed -n '500,555p' tests/agent/test_session_manager_history.py sed -n '770,835p' nanobot/agent/memory.py - Assistant
复现机制已确认:当最近用户在
len(tail)-max_messages之前时,方法保留的是一个非尾部切片(如[85:95]),而调用方却把它当成真正后缀并以长度反推cut=90,造成[85:90]重叠;还会丢失末尾[95:100]。issue 明确期望保留从最近用户到结尾,即必要时允许超过软目标max_messages。我再检查所有调用方和项目版本状态,判断是让方法返回边界,还是恢复“真正后缀”的不变量(后者也能让现有调用安全)。执行:shellgit status --short grep -R "\.retain_recent_legal_suffix" -n nanobot tests --exclude-dir=__pycache__ grep -R "compact_idle_session" -n tests git log -8 --oneline -- nanobot/session/manager.py nanobot/agent/memory.py - Assistant
retain_recent_legal_suffix同时服务文件硬上限和 idle compact:前者确实需要硬上限,后者要求对话边界完整。最稳妥的修复是让方法只产生连续后缀并显式返回(dropped, already_consolidated),如此调用方无需猜边界;但 issue 的具体期望要求 else 分支保留 15 条,与现有“hard cap”测试冲突。先用 blame/提交差异确认设计演变,尤其接口描述是否对应预期的新 API。执行:shellgit blame -L 260,340 nanobot/session/manager.py git show 48d35bd2^:nanobot/session/manager.py | grep -n -A65 -B5 "retain_recent_legal_suffix" git show 48d35bd2 -- nanobot/session/manager.py nanobot/agent/memory.py - Assistant
接口描述明确要求返回
(dropped_messages, already_consolidated_count),这正是消除“按长度猜切点”的通用方案。我将同时恢复关键不变量:retained必须始终是原列表的连续末尾后缀;else 分支从latest_user一直保留到结尾,而不是截成前向窗口。这样会按 issue 要求在单轮特别长时允许超过目标值;这是保持完整上下文所必需,旧的“绝对硬上限”测试与新需求冲突,后续测试结果中会明确辨别。实现前先确认
find_legal_message_start的行为,随后修改方法、文件归档调用方和 idle compact 调用方。执行:shellgrep -R "def find_legal_message_start" -n nanobot sed -n '1,100p' nanobot/utils/helpers.py sed -n '1010,1045p' nanobot/cli/commands.py sed -n '310,455p' tests/agent/test_consolidator.py - Assistant
上一个输出未覆盖 helper 的目标行;先读取该函数,再一次性编辑。执行:
shellsed -n '225,280p' nanobot/utils/helpers.py - Assistant
- Assistant
现在运行最小复现,直接验证 issue 给出的 100/85/10 场景中边界恰为 85、两部分无对象重叠、拼接可还原原历史;同时覆盖
last_consolidated返回计数和无用户消息场景。随后运行相关测试文件。执行:shell - Assistant
最小复现通过:得到
dropped=85、kept=15,且dropped + kept完整还原原历史。相关测试 56 个中 55 个通过,唯一失败是旧测试强制len <= max_messages;它与 issue 明确要求的例子(max=10但保留 15 条)直接冲突,因此属于需求变更后应更新的断言,不能为了通过它重新丢弃用户锚点或末尾响应。接下来检查文件 cap 测试及类型/格式工具配置,并运行更广泛的 session/auto-compact 测试,以确保返回类型变更没有遗漏调用方。执行:
shellgrep -R "enforce_file_cap" -n tests nanobot --exclude-dir=__pycache__ grep -nE '^\[tool\.(ruff|mypy)|ruff|mypy' pyproject.toml pytest -q tests/agent/test_auto_compact.py tests/agent/test_autocompact_unit.py - Assistant
enforce_file_cap的两类归档计数测试已随 auto-compact 套件通过,说明新返回值对 consolidated/unconsolidated 边界处理正确。现在运行 Ruff 和完整测试套件;预期仅保留已识别的旧“绝对硬上限”冲突,如出现其他失败则继续修复。执行:shellruff check nanobot/session/manager.py nanobot/agent/memory.py pytest -q - Assistant
完整套件结果为 3774 通过、3 跳过、2 失败:
- 已说明的旧硬上限断言,与新 issue 的明确期望冲突;
- MCP preset API 测试,与本次改动文件/功能无关,需单独重跑判断是否为套件顺序污染或环境问题。
Ruff 已通过。现在单独重跑无关失败,并复核最终 diff、未跟踪文件和调用点,确保没有测试或临时文件被修改。执行:
shellpytest -q tests/webui/test_mcp_presets_api.py::test_test_mcp_preset_connects_and_reports_tools git status --short git diff --check git diff --stat grep -R "retain_recent_legal_suffix(" -n nanobot --exclude-dir=__pycache__
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.