Skip to content
TheChatArchiveThe Chat Archive
GLMz-ai/glm-5.2· unknownHugging 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. GLM Assistant

    Let me start by exploring the repository structure and understanding the codebase.

  3. GLM Assistant

    Let me see the full retain_recent_legal_suffix method and the surrounding code.

  4. GLM Assistant

    Now I understand the structure. The issue is in enforce_file_cap. Let me analyze:

    In enforce_file_cap:

    1. before = list(self.messages) — full list
    2. self.retain_recent_legal_suffix(limit) — this modifies self.messages to keep only the retained suffix
    3. dropped_count = before_count - len(self.messages) — number of dropped messages
    4. dropped = before[:dropped_count] — first dropped_count messages
    5. archive_chunk = dropped[already_consolidated:]

    The problem: When the else branch is triggered (tail is all assistant/tool messages), retain_recent_legal_suffix anchors to latest_user in the full session. But dropped_count = before_count - len(self.messages) doesn't necessarily match the actual cut point.

    Let me think with the example from the issue:

    • tail = messages[0:100] (100 messages)
    • latest_user = 85 (user message at position 85)
    • max_messages = 10

    In the else branch:

    • retained = self.messages[85:95] (10 messages: positions 85-94)
    • Then find_legal_message_start, etc.
    • After this, self.messages = retained (10 messages)
    • dropped = len(self.messages_before) - len(retained) = 100 - 10 = 90
    • dropped = before[:90] = positions 0-89

    But wait — positions 85-89 are part of the retained messages (positions 85-94 are retained). So dropped[:90] = positions 0-89 OVERLAPS with retained positions 85-94!

    So the user message at position 85 is in BOTH dropped (archive) and retained (kept). That's the bug.

    The issue says cut should be latest_user (85), not 90. So archive = before[:85] and kept = before[85:].

    So the fix is: the retain_recent_legal_suffix should return the actual cut point / dropped messages, so that enforce_file_cap can compute the archive correctly.

    The interface description says:

    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.
    

    So the expected behavior is that retain_recent_legal_suffix should return tuple[list[dict], int] — the dropped messages (the actual dropped prefix, not computed by before_count - len(retained)) and a count of how many were already consolidated.

    Let me look at how retain_recent_legal_suffix is called currently and what the tests expect.

  5. GLM Assistant

    Now let me understand the current behavior and the bug better. Let me look at the test for enforce_file_cap.

  6. GLM Assistant

    Let me look at the test helpers and more tests.

  7. GLM Assistant

    Let me look at the other callers to understand the max_messages values used.

  8. GLM Assistant

    Let me look at the archive/consolidation logic to understand what archive expects.

  9. GLM Assistant

    Let me look at raw_archive and archive to understand what they expect.

  10. GLM Assistant

    Now let me look at _consolidate_replay_overflow to understand the memory.py usage pattern.

  11. GLM Assistant

    Let me check the tests related to _idle_compact / idle session compaction.

  12. GLM Assistant

    Let me search for _RECENT_SUFFIX_MESSAGES and understand the default 10 reference.

  13. GLM Assistant

    Let me look at the consolidator tests that use the real compact_idle_session.

  14. GLM Assistant

    Now I have a thorough understanding. Let me write a reproduction script first to confirm the bug.

  15. GLM Assistant

    The bug is confirmed — u85 (user message) and a86-a89 appear in both kept and archived. Now let me implement the fix. First, let me view the exact current code.

  16. GLM Assistant

    Now let me implement the fix for retain_recent_legal_suffix:

  17. GLM Assistant

    Now let me update enforce_file_cap to use the returned dropped list:

  18. GLM Assistant

    Now let me update compact_idle_session (the _idle_compact method) in memory.py, which has the identical bug pattern:

  19. GLM Assistant

    Now let me run the reproduction script again to confirm the fix:

  20. GLM Assistant

    The fix works — no overlap, u85 only in kept, archive ends at a84. Now let me run the existing tests to ensure nothing breaks.

  21. GLM Assistant

    All 142 tests pass. Now let me write a more thorough test to verify the compact_idle_session path (memory.py) and edge cases:

  22. GLM Assistant

    Both paths work. Let me test edge cases comprehensively:

  23. GLM Assistant

    All edge cases pass. Now let me run the broader test suite to ensure nothing else is affected:

  24. GLM Assistant

    One test failed, but it's in test_mcp_presets_api.py which is unrelated to my changes. Let me confirm it's a pre-existing failure by checking it against the original code:

  25. GLM Assistant

    Confirmed — the MCP presets test fails on the original code too (pre-existing, environment-related, unrelated to my change). Let me review my final changes:

  26. GLM Assistant

    Let me verify the commands.py caller (which ignores the return value) still works fine, and check there are no type-checking issues:

  27. GLM Assistant

    Let me run the directly-related tests one more time and clean up temp files:

  28. GLM Assistant

    All tests pass and the bug is fixed. Let me do a final review of the complete modified retain_recent_legal_suffix and enforce_file_cap:

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