Skip to content

fix(truncator): keep an all-system context in the system half#8855

Open
he-yufeng wants to merge 1 commit into
AstrBotDevs:masterfrom
he-yufeng:fix/truncator-all-system-split
Open

fix(truncator): keep an all-system context in the system half#8855
he-yufeng wants to merge 1 commit into
AstrBotDevs:masterfrom
he-yufeng:fix/truncator-all-system-split

Conversation

@he-yufeng

@he-yufeng he-yufeng commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

What

ContextTruncator._split_system_rest splits a message list into (system_messages, rest). It initialized first_non_system = 0 and only updated it when it found a non-system message. When every message is a system message the loop never breaks, so first_non_system stays 0 and the split is reversed — the system messages end up in the rest half and the system half is empty.

The truncation strategies then treat those system messages as droppable turns. For example truncate_by_halving on four system messages returns only two:

msgs = [Message(role="system", content=f"S{i}") for i in range(4)]
ContextTruncator().truncate_by_halving(msgs)
# before: 2 messages (two system messages dropped)
# after:  4 messages (all preserved)

System messages must never be dropped by truncation. The fix defaults first_non_system to len(messages), so an all-system list stays entirely in the system half. Mixed inputs (the loop still breaks at the first non-system message) and empty inputs are unchanged.

Test plan

Added two tests in tests/agent/test_truncator.py:

  • test_split_system_rest_with_only_system_messages — all-system input puts everything in the system half.
  • test_halving_keeps_all_system_messagestruncate_by_halving keeps all four system messages.

Both fail on master and pass with this change.

python -m pytest tests/agent/test_truncator.py
# 37 passed

ruff check and ruff format --check pass on the changed files.

Summary by Sourcery

Ensure context truncation never drops system messages when splitting or halving message histories.

Bug Fixes:

  • Correct splitting of message lists so all-system inputs are fully retained in the system portion instead of being misclassified as non-system messages.

Tests:

  • Add tests verifying that all-system message lists remain entirely in the system half and that halving truncation preserves all system messages.

_split_system_rest initialized first_non_system to 0, so when every message is
a system message the loop never breaks and the split is reversed: the system
messages land in the non-system half. truncate_by_halving then treats them as
droppable turns and discards half of them (e.g. 4 system messages -> 2). System
messages must never be dropped. Default first_non_system to len(messages) so an
all-system list stays entirely in the system half; mixed and empty inputs are
unchanged.
@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. area:core The bug / feature is about astrbot's core, backend labels Jun 18, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request fixes a bug in the context truncator where an all-system message list was incorrectly split, potentially leading to system messages being dropped during truncation. The fix initializes the split index to the length of the messages, ensuring all-system lists are correctly handled. Corresponding unit tests have been added to verify this behavior. No review comments were provided, so there is no feedback to address.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • The new tests exercise behavior via the private helper _split_system_rest; consider adding/adjusting a test that validates the same invariant purely through the public truncation APIs so future refactors of the helper don’t require test changes.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new tests exercise behavior via the private helper `_split_system_rest`; consider adding/adjusting a test that validates the same invariant purely through the public truncation APIs so future refactors of the helper don’t require test changes.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core The bug / feature is about astrbot's core, backend size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant