Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-sonnet-5· anthropicHugging Face ibragim-bad/swe_rebench_07_2026_trajectories

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

  1. 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

    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.

    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 git checkout <file> to restore the file to its original state. You can also reset all changes in the repository using git reset --hard command. Additionally, you can use git stash and git stash pop 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 submit) using git status or git diff commands. However you don't need to use git add or git commit before submitting your changes. In fact, submit only submits currently unstaged changes because it uses git diff to get the changes that need to be submitted.

    CHECKLIST

    Before submitting your solution, please go over the following checklist and make sure you've done everything:

    • If an error was reported in the issue description, I have successfully reproduced it.
    • If an error was reported in the issue description, I have confirmed that my fix resolves the error.
    • I have thought about edge cases and made sure my fix handles them as well.
    • I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.

    Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!

    (Current directory: /nanobot, current file: none) bash-$

  2. Claude Assistant

    Now let's view full function context and find_legal_message_start to confirm usage, then write the replacement.

  3. Claude Assistant

    Now let's write the new implementation. First replace the function body.

  4. Claude Assistant

    Now let's replace lines 272-342 with the fixed implementation.

  5. Claude Assistant

    Let me use the heredoc syntax instead.

  6. Claude Assistant

    Now let's update the callers: memory.py's hard_truncate_idle and cli/commands.py.

  7. Claude Assistant

    Now let's check further down how kept/cut might be used later in that function, e.g. for updating session.messages.

  8. Claude Assistant

    Good, this is correct and consistent - session.messages = kept (probe.messages after trimming), which no longer overlaps with archive_msgs. Now let's check cli/commands.py usage.

  9. Claude Assistant

    This call ignores the return value, which is fine since it's a statement call - Python allows ignoring return values.

  10. Claude Assistant

    Now let's re-run the reproduction script and existing tests.

  11. Claude Assistant

    User message now correctly preserved in kept, no overlap, no data loss. Now let's run the full test suite for session manager and memory.

  12. Claude Assistant

    All pass. Now let's run broader test suite touching memory.py and session manager.

  13. Claude Assistant

    All pass. Let's check test_auto_compact.py's usage at line 79 to make sure it's compatible.

  14. Claude Assistant

    Confirmed pre-existing failure unrelated to our change. Now let's double check the diff and also check other places relying on Any import for enforce_file_cap signature etc.

  15. Claude Assistant

    This confirms the fix keeps all messages accounted for (no loss, no duplication) and enforces the cap correctly. Now let's run the full targeted test files once more plus a quick sanity check on memory.py's Consolidator idle-truncate function directly.

  16. Claude Assistant

    Let's check test_auto_compact.py for tests exercising compact_idle_session directly (real code, not the fake helper).

  17. Claude Assistant

    End-to-end works correctly: 90 messages archived, 10 kept, user message preserved as the start of kept, no duplication. Let's run the full test suite once more for final confirmation and clean up.

  18. Claude Assistant

    All tests pass. Let's also check the CLI heartbeat call site doesn't need changes.

  19. Claude Assistant

    All good. Let's review the checklist explicitly.

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