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
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_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
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto 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-$
Assistant
Let me analyze the issue. The bug is in
nanobot/session/manager.pyin theretain_recent_legal_suffixmethod. The problem is that when the else branch is triggered (recent N messages are all assistant/tool messages), thecutvalue calculation useslen(tail) - len(kept)without considering the actual position of the user message, causing user messages to be incorrectly archived to both archive and kept.Let me first explore the repository structure and find the relevant file.
Assistant
The error says I can't extract the command. Let me check what's happening - I need to use the correct format.
Actually, looking at the error message, it seems like the tool call format might not be right. Let me try again with the proper format.
Assistant
Let me search for the specific method mentioned in the issue:
Assistant
Now I can see the method. But wait - the issue description says the function returns
tuple[list[dict], int]but the current implementation returnsNone. Let me also look at how it's called and understand the full context better. Let me also check if there's a different version of this method or if the issue refers to a different function signature.Assistant
Let me look at the test file to understand the expected behavior better:
Assistant
Now let me understand the issue better. The issue description mentions that the method returns
tuple[list[dict], int]but looking at the code, it returnsNone. However, re-reading the issue more carefully, I see the issue is about theenforce_file_capmethod and how it interacts with the retained messages. Let me look more carefully.Actually, wait - let me re-read the issue. The issue says the method signature should be
retain_recent_legal_suffix(self, max_messages: int) -> tuple[list[dict], int]. The current code returnsNone. So the method needs to be refactored to return the dropped messages and the count.But actually, looking more carefully at the bug description, the core issue is:
- In the
elsebranch, when the recent N messages are all assistant/tool messages (no user message) - The code finds
latest_userin the full message list - But then
retained = list(self.messages[latest_user: latest_user + max_messages])- this creates a new retained list starting fromlatest_user - The problem is that
dropped = len(self.messages) - len(retained)calculates the wrong number of dropped messages
Wait, let me re-read the issue again. The issue mentions:
- "用户消息同时出现在 archive 和 kept 中" - user message appears in both archive and kept
- The
cutvalue calculation useslen(tail) - len(kept)without considering the actual user message position
Let me look at the
enforce_file_capmethod more closely:- In the
Assistant
Now I understand the issue better. Let me trace through the bug scenario:
self.messageshas 100 messages (indices 0-99)max_messages= 10retained = self.messages[-10:]→ messages at indices 90-99 (all assistant/tool)- Since no user message in
retained, enterelsebranch - Find
latest_userin full session, say it's at index 85 retained = self.messages[85:85+10]→ messages at indices 85-94 (95 messages dropped from the front: 0-84)
But here's the problem:
dropped = len(self.messages) - len(retained)= 100 - 10 = 90self.messages = retained→ now self.messages has indices 85-94- But we dropped indices 0-84 AND 95-99!
Wait, actually the retained list replaces self.messages entirely. So the messages at indices 95-99 are lost (not preserved). But actually, in
enforce_file_cap,before = list(self.messages)captures the original. Anddropped = before[:dropped_count]takes the firstdropped_countmessages frombefore.Let me re-think. The issue is more subtle:
Scenario:
self.messageshas 100 messages, indices 0-99- Last 10 messages (90-99) are all assistant/tool
- The last user message is at index 85 (which is in positions 0-89 of the original list)
max_messages= 10
Step by step:
retained = self.messages[-10:]→ [msg90, msg91, ..., msg99]first_user = None(no user in retained)- Enter else:
latest_user = 85 retained = self.messages[85:85+10]→ [msg85, msg86, ..., msg94]dropped = len(self.messages) - len(retained)= 100 - 10 = 90self.messages = retained→ [msg85, ..., msg94]self.last_consolidated = max(0, self.last_consolidated - 90)
In
enforce_file_cap:before = [msg0, msg1, ..., msg99]dropped_count = 90dropped = before[:90]→ [msg0, ..., msg89]- These are the "archived" messages
BUT the problem is:
self.messagesafter retain = [msg85, ..., msg94]dropped= [msg0, ..., msg89]- msg85 (the user message) appears in BOTH
self.messagesANDdropped!
That's the bug! The user message at index 85 is:
- In
retained(self.messages after trim): [msg85, ..., msg94] - In
dropped(for archiving): [msg0, ..., msg89] which includes msg85
So msg85 is in both places, which means the user message gets both archived AND kept.
The fix should be: when in the else branch,
droppedshould only include messages beforelatest_user, not all the way up tolen(retained).The issue description says:
cut = latest_user # 应该是 85,不是 90 archive = tail[:85] # [0-84],不包含用户消息 kept = tail[85:] # [85-99],包含用户消息及其后续内容So the fix is to change the calculation of
droppedto account for the actual start position of the retained messages. The retained messages start at indexlatest_user(85), so:droppedshould beself.messages[:latest_user]= [msg0, ..., msg84]self.messagesshould beself.messages[latest_user:]trimmed to max_messages
But wait, the current code does
retained = self.messages[latest_user: latest_user + max_messages]which could be shorter than the remaining messages. Let me think about this more carefully.Actually, looking at the code flow:
After
retained = list(self.messages[latest_user: latest_user + max_messages]):- retained starts at
latest_userand has at mostmax_messageselements dropped = len(self.messages) - len(retained)
The issue is that
droppedshould belatest_user(the index where retained starts), notlen(self.messages) - len(retained).Because:
- Messages before
latest_user(indices 0 to latest_user-1) should be dropped - Messages from
latest_useronwards should be kept (up to max_messages) - But currently,
dropped = len(self.messages) - len(retained)could include messages that are actually in retained
Let me create a test script to reproduce the bug:
Assistant
The test didn't reproduce the bug because the last user message at index 60 is within the tail of 10 messages (indices 60-69). Let me adjust the test to match the exact scenario described in the issue:
Assistant
I've confirmed the bug. The user message
user_msg_LAST_85appears in both thedropped(archive) list and thekeptlist. This is exactly what the issue describes.Now let me understand the root cause and fix:
Root cause analysis:
retained = self.messages[-10:]→ messages at indices 90-99 (all assistant)first_user = None(no user in retained), enter else branchlatest_user = 85retained = self.messages[85:85+10]→ [msg85, msg86, ..., msg94] (10 messages)dropped = len(self.messages) - len(retained)= 100 - 10 = 90self.messages = retained→ now contains msg85-msg94- In
enforce_file_cap:dropped = before[:90]→ [msg0, ..., msg89]
The problem:
dropped_count = 90but the actual start index of retained is 85. So messages at indices 85-89 are counted as both dropped (inbefore[:90]) AND kept (inself.messages).Fix: The
dropped_countshould be the index where retained starts, which islatest_user(85), notlen(self.messages) - len(retained)(90).The fix should track the actual cut point. Let me look at the method signature in the issue description again:
The issue says the method should return
tuple[list[dict], int]- the dropped messages and a count of how many were already consolidated. This suggests a more significant refactor.But looking at the current code, the simplest fix would be to track the actual cut index. Let me look at the method again:
Assistant
Now I see the issue clearly. The problem is on the line:
dropped = len(self.messages) - len(retained)This assumes that
retainedis a suffix ofself.messages. But in theelsebranch,retainedcan beself.messages[latest_user: latest_user + max_messages], which is a window in the middle of the list, not a suffix.The fix should track the actual cut index in
self.messages. Let me trace through all code paths:Path 1:
first_user is not Noneretained = self.messages[-max_messages:]→ a suffixretained = retained[first_user:]→ still conceptually a suffix starting from indexlen(self.messages) - max_messages + first_user- After
find_legal_message_start: retained could be trimmed further from the front - The cut point in original self.messages =
len(self.messages) - len(retained)
Path 2:
elsebranch withlatest_user is not Noneretained = self.messages[latest_user: latest_user + max_messages]- After
find_legal_message_start: retained could be trimmed further from the front - The cut point in original self.messages =
latest_user + (trim from find_legal_message_start)
Path 3:
elsebranch withlatest_user is None- No user message found, retained stays as the last max_messages
The key insight is that we need to track the actual starting index in
self.messagesfor the retained messages. Let me implement the fix:Assistant
Now I need to track the actual cut index in
self.messages. The approach is to track whereretainedstarts in the originalself.messageslist. Let me implement the fix:Assistant
Now let me apply this fix to the actual file:
Assistant
I see there's a duplication issue - the last two lines got duplicated. Let me fix that:
Assistant
I see the duplicated lines. Let me remove them:
Assistant
Good, the duplicate lines are removed. Now let me verify the fix by running the test script again:
Assistant
The bug is still present! The issue is that my fix only fixes the
droppedcount insideretain_recent_legal_suffix, but theenforce_file_capmethod calculatesdropped_countindependently asbefore_count - len(self.messages). So even thoughself.last_consolidatedis now adjusted correctly, thedroppedlist inenforce_file_capstill includes the overlap.Let me look at
enforce_file_capmore carefully:Assistant
I see the issue now. The
enforce_file_capmethod computesdropped_count = before_count - len(self.messages), which is 100 - 10 = 90. Thendropped = before[:90]takes the first 90 messages.But with my fix,
retain_recent_legal_suffixsetsdropped = 85(theretained_offset), which correctly adjustsself.last_consolidated. However, thedropped_countinenforce_file_capis still 90, anddropped = before[:90]still includes the user message.The problem is that
enforce_file_capuses its own calculation ofdropped_countbased on length difference, rather than knowing which messages were actually dropped from the original list.There are two approaches to fix this:
-
Make
retain_recent_legal_suffixreturn the actual drop count - The issue description mentions the method should returntuple[list[dict], int]. This would requireenforce_file_capto use the returned value. -
Store the drop count as an instance variable - Less clean but backward-compatible.
Let me look at the issue description again. It says:
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.This suggests changing the method to return the dropped messages and the count. Let me implement this approach:
-
Assistant
The key issue is: the
enforce_file_capmethod needs to know which messages were actually dropped (by index), not just the count. Since it capturesbefore = list(self.messages)before callingretain_recent_legal_suffix, and the method modifiesself.messagesin place, we need a way to communicate the actual dropped index.Let me change the approach: make
retain_recent_legal_suffixreturn a tuple of(dropped_messages, consolidated_count)so callers can use the exact dropped messages. But this would be a bigger refactor.Actually, let me think about this more carefully. The simplest fix that doesn't change the interface is to make
retain_recent_legal_suffixalso set an attribute thatenforce_file_capcan use. But that's hacky.A cleaner approach: the actual issue is that
enforce_file_capcomputesdropped_countincorrectly. Sincebefore[:dropped_count]would include messages that are now inself.messages, we should use the intersection properly.Wait, I think the cleanest fix is:
- Keep my fix in
retain_recent_legal_suffix(trackingretained_offset) - Also make
retain_recent_legal_suffixreturn(dropped_messages, consolidated_count)as the issue suggests - Update
enforce_file_capto use the returned dropped messages directly
But looking at the other callers, they don't use the return value:
nanobot/agent/memory.py:810: probe.retain_recent_legal_suffix(max_suffix)nanobot/cli/commands.py:1032: session.retain_recent_legal_suffix(hb_cfg.keep_recent_messages)
So changing the return type would require updating all callers. Let me check if that's acceptable by looking at how the callers use it:
- Keep my fix in
Assistant
AH HA! This is the exact bug in
memory.py! It computescut = len(tail) - len(kept), which has the same problem! Let me see more context:Assistant
OK, so the callers in
memory.pyandenforce_file_capboth have the same pattern of computingcut = len(tail) - len(kept)which causes the bug. The callers incli/commands.pydon't use the returned value.The cleanest fix is to change
retain_recent_legal_suffixto return the actual drop count (or the dropped messages) so callers can use it correctly. Let me implement this:Assistant
Now I need to update
enforce_file_capto use the returned value fromretain_recent_legal_suffix:Assistant
Now I need to update
enforce_file_capto use the returneddropped_countfromretain_recent_legal_suffix:Assistant
Now I need to also fix
memory.pywhich has the same bug pattern. Let me look at it:Assistant
Good. Now let me verify the fix by running the test script again:
Assistant
The test script still shows the bug because it computes
dropped_countusing the old formulabefore_count - len(session.messages)instead of using the return value. Let me update the test script to simulate the fix properly:Assistant
The fix works. Now let me run the existing tests to make sure I haven't broken anything:
Assistant
All 29 tests pass. Let me also run the auto_compact tests since I modified
memory.py:Assistant
All 50 tests pass. Let me also run any other tests that might be related to session management:
Assistant
All 253 session-related tests pass. Let me also write a more comprehensive edge case test to make sure my fix handles all scenarios:
Assistant
All edge cases pass. Let me now verify the complete changes I've made by checking the git diff:
Assistant
The changes look good. Let me run a broader test suite to make sure everything still works:
Assistant
All 1052 agent tests pass. Let me verify the final state of the changes:
Assistant
Let me review the final changes and run one more validation test before submitting:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →