Skip to content

fix(context_compressor): prevent consecutive same-role messages after compression#1186

Closed
alireza78a wants to merge 1 commit intoNousResearch:mainfrom
alireza78a:fix/context-compressor-role-collision-v2
Closed

fix(context_compressor): prevent consecutive same-role messages after compression#1186
alireza78a wants to merge 1 commit intoNousResearch:mainfrom
alireza78a:fix/context-compressor-role-collision-v2

Conversation

@alireza78a
Copy link
Contributor

compress_context() picks the summary message role based only on the message before the compression window. If the first message after the window has the same role, the API returns 400 — consecutive same-role messages aren't allowed. This causes long conversations to crash mid-reply with no useful error, forcing the user to /reset and lose their session history.

Fix checks both neighbors when choosing the summary role. If flipping still collides with the head, falls back to "user" with a short prefix.

teknium1 added a commit that referenced this pull request Mar 17, 2026
… compression

compress() checks both the head and tail neighbors when choosing the
summary message role.  When only the tail collides, the role is flipped.
When BOTH roles would create consecutive same-role messages (e.g.
head=assistant, tail=user), the summary is merged into the first tail
message instead of inserting a standalone message that breaks role
alternation and causes API 400 errors.

The previous code handled head-side collision but left the tail-side
uncovered — long conversations would crash mid-reply with no useful
error, forcing the user to /reset and lose session history.

Based on PR #1186 by @alireza78a, with improved double-collision
handling (merge into tail instead of unconditional 'user' fallback).

Co-authored-by: alireza78a <alireza78.crypto@gmail.com>
teknium1 added a commit that referenced this pull request Mar 17, 2026
… compression (#1743)

compress() checks both the head and tail neighbors when choosing the
summary message role.  When only the tail collides, the role is flipped.
When BOTH roles would create consecutive same-role messages (e.g.
head=assistant, tail=user), the summary is merged into the first tail
message instead of inserting a standalone message that breaks role
alternation and causes API 400 errors.

The previous code handled head-side collision but left the tail-side
uncovered — long conversations would crash mid-reply with no useful
error, forcing the user to /reset and lose session history.

Based on PR #1186 by @alireza78a, with improved double-collision
handling (merge into tail instead of unconditional 'user' fallback).

Co-authored-by: alireza78a <alireza78.crypto@gmail.com>
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.

1 participant