This repository was archived by the owner on Aug 21, 2026. It is now read-only.
Repository navigation
fix: trim oldest research turns on token limit during compression - #332
Closed
bigfish-emily wants to merge 1 commit into
Closed
bigfish-emily wants to merge 1 commit into
bigfish-emily wants to merge 1 commit into
Conversation
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Fixes #252.
When
compress_researchhits a token-limit error, the retry path is meant tofree context by removing the older research history so the most recent
findings are preserved. The old
remove_up_to_last_ai_messagehelper did theopposite: it dropped the last AI message and everything after it, which
keeps the oldest (and largest) tool outputs, so repeated retries rarely freed
meaningful space. It also removed the
compress_research_simple_human_messageinstruction that was appended just before the retry loop, so subsequent
attempts ran without the compression-mode instruction.
Additionally, the token-limit detector was handed
research_modeleven thoughthe failing call uses
compression_model; when the two models come fromdifferent providers, provider-specific error matching could miss the limit
error entirely.
What Changed
remove_oldest_research_turninsrc/open_deep_research/utils.py,which removes the oldest complete research turn (the first AI message plus
its immediately following
ToolMessageresults) while preserving the leadingHumanMessagetopic, the newest findings, and the trailing compressioninstruction. Message alternation stays valid so the retried model call is
well-formed.
compress_researchto use the new helper and to passconfigurable.compression_modeltois_token_limit_exceeded.tests/test_utils.pycovering turn removal, single-AI transcripts,no-AI transcripts, structural validity, and repeated truncation.
Verification
The new unit tests were executed against the helper implementation; all five
pass:
python -m py_compilealso succeeds on all modified files.