Skip to content

Conversation

@xingdi-eric-yuan
Copy link
Collaborator

Refactors trim_prompt_messages and trim functions in debug_gym/llms/utils.py for improved readability and correctness.

trim_prompt_messages changes:

  • Unified pair handling for both tool-based and simple conversations
  • Simplified pair detection to role in ["user", "tool"] (works with both OpenAI and Anthropic formats)
  • Clearer priority order: system message > first user message (task description) > most recent (assistant, response) pairs

trim changes:

  • Split parameterized find_char_position_for_tokens into two focused helpers: find_max_prefix_length and find_min_suffix_start
  • Added clearer docstring and inline examples

Tests:

  • Reorganized into 4 test classes covering validation, tool-based, simple conversations, and Anthropic format
  • Added test coverage for Anthropic-style tool results

xingdi-eric-yuan and others added 2 commits January 14, 2026 10:52
- Simplify pair detection: unified (assistant, user/tool) pairs for both
  tool-based and simple conversations
- Fix priority: system > user (task description) > most recent pairs
- Remove unused `get_message_tokens` and `is_tool_result_message` helpers
- Add comprehensive test coverage organized by scenario type:
  - Validation tests
  - Tool-based conversation tests
  - Simple conversation tests
  - Anthropic format tests
- Remove redundant final token count verification

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Split the parameterized find_char_position_for_tokens helper into two
focused functions: find_max_prefix_length and find_min_suffix_start.
Added clearer docstring explaining the binary search approach and
inline examples showing what each trim mode produces.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@matheper
Copy link
Collaborator

In generate_with_drop_message_and_retry, can you keep reducing the context_length based on the number of retries when calling trim_prompt_messages to avoid getting in this situation where it's Unable to reduce prompt size. trim_prompt_messages returned the same messages.

Copy link
Collaborator

@matheper matheper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@xingdi-eric-yuan xingdi-eric-yuan merged commit 10a3b8b into main Jan 15, 2026
16 of 17 checks passed
@xingdi-eric-yuan xingdi-eric-yuan deleted the rewrite_trim branch January 15, 2026 16:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants