Skip to content
TheChatArchiveThe Chat Archive
composer-2.5· unknownHugging Face ibragim-bad/swe_rebench_07_2026_trajectories

Bug: retainrecentlegalsuffix 在 else 分支下会导致用户消息被重复归档 Bug Description 在…

  1. User

    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

    1. 会话中消息数量超过 _RECENT_SUFFIX_MESSAGES (默认10条)
    2. 最近 N 条消息中 不包含任何用户消息 (全是 assistant/tool 角色)
    3. 在完整会话历史中,最后一条用户消息位于倒数第 11-15 条之间

    Expected Behavior

    当会话历史中最近 N 条消息全是助手/工具消息,而用户消息位于更早位置时:

    1. 用户消息不应被重复归档 - 用户消息应该只保留在 kept 中,不应该同时出现在 archive 和 kept 中
    2. 归档边界应该正确 - cut 值应该基于用户消息的实际位置,而不是简单的 len(tail) - len(kept)
    3. 上下文连贯性应该保持 - 从用户消息开始的完整上下文应该被保留,不应该被错误切割

    假设:

    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 中 ✅

    上下文连贯 ✅

    总结

    期望行为 :无论用户消息位于何处,它都应该:

    1. 只出现在 kept 中(保留消息)
    2. 不出现在 archive 中(归档消息)
    3. 作为保留消息的起始点,确保上下文连贯

    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_messages while 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.

  2. Assistant

    Investigating retain_recent_legal_suffix and its tests to fix duplicate archiving when the recent suffix has no user messages.

  3. Assistant

    The bug report references a different return signature; searching for the archive/kept split logic.

  4. Assistant

    The bug is that callers assume kept is a tail suffix, but the else branch anchors at latest_user. I'll update retain_recent_legal_suffix to return the actual dropped messages and fix callers.

  5. Assistant

    Implementing the fix: tracking keep_start explicitly and returning dropped messages.

  6. Assistant

    Adding a regression test for the bug scenario:

  7. Assistant

    已修复 retain_recent_legal_suffix 在 else 分支下导致用户消息被重复归档的问题。

    根因

    当最近 N 条消息全是 assistant/tool(没有 user)时,方法会锚定到 latest_user 并保留 messages[latest_user: latest_user + max_messages]。但调用方(memory.py 的 idle compact、enforce_file_cap)用 cut = len(tail) - len(kept) 计算归档边界,隐含假设 kept 一定是 tail 的后缀。

    例如 100 条消息、latest_user=85、max_messages=10 时:

    • 实际保留:[85:95]
    • 错误 cut:100 - 10 = 90
    • 错误归档:[0:90],与用户消息 [85:89] 重叠

    修复

    1. retain_recent_legal_suffix 显式跟踪 keep_start,返回 (dropped, already_consolidated),不再依赖 len(tail) - len(kept) 推算。
    2. enforce_file_cap 和 compact_idle_session 改为使用返回值,直接拿到正确的归档消息列表。

    行为验证

    新增回归测试 test_retain_recent_legal_suffix_no_overlap_when_user_before_tail:

    • dropped == before[:85](不含用户消息)
    • kept 从 "anchor" 用户消息开始
    • archive 与 kept 无重叠

    相关 53 个测试均已通过。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.