Skip to content

fix(agent): retag a folded compaction head as a user turn - #141

Draft
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/critical-bug-identification-a887
Draft

cursor[bot] wants to merge 1 commit into
mainfrom
cursor/critical-bug-identification-a887

Conversation

@cursor

@cursor cursor Bot commented Nov 6, 2026

Copy link
Copy Markdown

Bug and impact

Session compaction can fold a summary into an assistant message and leave that role in place. With an odd max_context_messages, the stored count on the first compaction is max_ctx + 1, so the folded head is inside the sent window. Anthropic rejects an assistant-first transcript with HTTP 400. The router treats 4xx as terminal, agent_run returns before session_save, and every later turn rebuilds the same request. The session accepts no new turns until /reset.

Trigger

max_context_messages is any odd value from 3 to 39 (the agent clamps above 40). The oldest stored user line is about 60 KiB (CLI -m or an ASAP task) and the following assistant reply is a few KiB, so the two summary lines do not fit in the 64 KiB source. After enough later turns that the stored count exceeds the limit, the next message sticks.

Root cause

compact_session_via_llm sets fold_into_oversized when a non-user line stops the summary pass at index 1, then fold_summary_into_message rewrites only content. The kept head stays assistant. The user-tail path already folds into a user message to avoid this Anthropic 400. The assistant-head path did not.

Fix

Retag that head to user, drop tool_use_id, and merge one following user turn so the roles still alternate. The oversized body stays in the head for the next stub pass.

Validation

test_compaction_fold_keeps_alternating_user_head failed before the fix (folded head role is assistant) and passes after it. CI=true make test_agent passes.

Open in Web View Automation 

An odd max_context_messages makes the first compaction window two
messages. When the assistant line does not fit the summary source, the
summary is folded into that line and the role stays assistant. Anthropic
rejects the transcript with HTTP 400, fallback stops, and the turn is
not saved, so every later turn rebuilds the same request.

Retag the folded head to user and merge the following user turn so the
roles still alternate.

Co-authored-by: esadrianno <esadrianno@gmail.com>

This branch has not been deployed

No deployments
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