Skip to content

Context overflow when compacting history#134

Merged
torrmal merged 2 commits intomainfrom
feature/context-owerflow
Apr 25, 2026
Merged

Context overflow when compacting history#134
torrmal merged 2 commits intomainfrom
feature/context-owerflow

Conversation

@tino097
Copy link
Copy Markdown
Contributor

@tino097 tino097 commented Apr 24, 2026

Fixes #106

Add three-tier ContextOverflowError recovery to session helpers

@tino097 tino097 requested a review from torrmal April 24, 2026 13:51
@entelligence-ai-pr-reviews
Copy link
Copy Markdown

entelligence-ai-pr-reviews Bot commented Apr 24, 2026

EntelligenceAI PR Summary

Refactors hard_truncate_history in anton/core/session.py and adds comprehensive unit tests in tests/test_chat.py to cover history truncation edge cases.

  • Replaced single-pass truncation logic with an iterative while loop in hard_truncate_history
  • Added handling to strip assistant-first entries and orphaned tool_result-only user messages at the head of retained history
  • Preserves non-tool_result content blocks in mixed-content user messages instead of dropping the entire message
  • Added loop break condition once a valid head message is found
  • Introduced four unit tests covering: no-op behavior, pair boundary preservation, orphaned tool-result regression, and mixed-content block filtering

Confidence Score: 5/5 - Safe to Merge

Safe to merge — this PR refactors hard_truncate_history in anton/core/session.py with an iterative while loop and adds proper handling for assistant-first entries and orphaned tool_result-only user messages at the head of retained history. The change also correctly preserves non-tool_result content blocks in mixed-content user messages rather than silently dropping entire messages, which is a meaningful correctness improvement. Comprehensive unit tests in tests/test_chat.py cover the edge cases introduced by this logic, and no review comments — new or pre-existing — flag any issues with this implementation.

Key Findings:

  • The iterative while loop in hard_truncate_history correctly handles the multi-pass nature of stripping assistant-first and orphaned tool_result entries, which the previous single-pass approach would have missed in chained edge cases.
  • Preserving non-tool_result content blocks in mixed-content user messages is a correctness fix over wholesale message removal, reducing potential silent data loss in conversation history.
  • New unit tests in tests/test_chat.py directly exercise the edge cases (orphaned tool_result messages, assistant-first head, mixed-content preservation) introduced by the refactor, providing meaningful regression coverage.
  • No review comments were generated and no pre-existing unresolved issues are flagged, giving high confidence that the implementation is clean and well-reasoned.
Files requiring special attention
  • anton/core/session.py
  • tests/test_chat.py

Copy link
Copy Markdown

@entelligence-ai-pr-reviews entelligence-ai-pr-reviews Bot left a comment

Choose a reason for hiding this comment

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

Refactors ChatSession in anton/core/session.py to centralize and improve context overflow recovery logic.

  • Introduced plan_with_recovery: non-streaming helper with three-tier recovery (normal → summarize+compact → hard-truncate)
  • Introduced plan_stream_with_recovery: streaming analogue that yields StreamContextCompacted events on compaction
  • Added hard_truncate_history: last-resort fallback that preserves only the tail of history while enforcing API message-ordering rules
  • Promoted _compacted_this_turn from a local variable to an instance variable, reset at the start of each turn, preventing double-summarization across both paths
  • Replaced all direct self._llm.plan and self._llm.plan_stream call sites in turn and _stream_and_handle_tools with the new helpers
  • Refactored task-completion verifier to use a messages_factory callable, ensuring post-compaction history is reflected on retry

Comment thread anton/core/session.py
Comment thread anton/core/session.py
@tino097
Copy link
Copy Markdown
Contributor Author

tino097 commented Apr 24, 2026

@Entelligence review

@entelligence-ai-pr-reviews
Copy link
Copy Markdown

Starting fresh pr review. This may take a few minutes

@entelligence-ai-pr-reviews
Copy link
Copy Markdown

New pr review completed! Check the pr for comments.

@tino097 tino097 requested review from ea-rus April 24, 2026 14:50
Copy link
Copy Markdown
Contributor

@torrmal torrmal left a comment

Choose a reason for hiding this comment

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

looks good to me

@torrmal torrmal merged commit a350ef8 into main Apr 25, 2026
6 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 25, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Context overflow not handled gracefully with local model (qwen3.5:35b, 65K context)

2 participants