Skip to content
This repository was archived by the owner on Aug 21, 2026. It is now read-only.
This repository was archived by the owner on Aug 21, 2026. It is now read-only.

Bug: compress_research removes recent messages instead of older ones on token limit #252

Open

Description

@ZG-666666

I found a possible inconsistency in the token-limit recovery logic used by compress_research.

In deep_researcher.py, when token limit is exceeded, the comment says this is handled by removing older messages:

except Exception as e:
    synthesis_attempts += 1

    # Handle token limit exceeded by removing older messages
    if is_token_limit_exceeded(e, configurable.research_model):
        researcher_messages = remove_up_to_last_ai_message(researcher_messages)
        continue

    # For other errors, continue retrying
    continue

However, remove_up_to_last_ai_message() in utils.py seems to do the opposite:

def remove_up_to_last_ai_message(messages: list[MessageLikeRepresentation]) -> list[MessageLikeRepresentation]:
    """Truncate message history by removing up to the last AI message.

    This is useful for handling token limit exceeded errors by removing recent context.

    Args:
        messages: List of message objects to truncate

    Returns:
        Truncated message list up to (but not including) the last AI message
    """
    # Search backwards through messages to find the last AI message
    for i in range(len(messages) - 1, -1, -1):
        if isinstance(messages[i], AIMessage):
            # Return everything up to (but not including) the last AI message
            return messages[:i]

    # No AI messages found, return original list
    return messages

This removes the last AI message and everything after it, which looks like removing the most recent context, not the older history.

If the intent here is to free context space while preserving the latest research progress, then this may be trimming the wrong side of the message history.

Could you confirm whether the intended behavior is:

  1. remove older messages and keep newer context, or
  2. roll back the most recent AI turn?

If the intent is the former, this may be a bug.

Activity

  1. changed the title [-]Possible bug: compress_research removes recent messages instead of older ones on token limit[/-] [+]Bug: compress_research removes recent messages instead of older ones on token limit[/+] on Mar 9, 2026
  2. added a commit that references this issue on Mar 12, 2026
    f463af4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions