Skip to content

fix(agent): keep history when compaction cannot finish the turn - #126

Closed
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/high-severity-issues-9b09
Closed

cursor[bot] wants to merge 1 commit into
mainfrom
cursor/high-severity-issues-9b09

Conversation

@cursor

@cursor cursor Bot commented Oct 16, 2026

Copy link
Copy Markdown

Summary

Compaction saved a summarized transcript before the user turn finished, and it could store two user turns in a row. A failed reply then deleted history the user still needed, and an odd max_context_messages made Anthropic reject that session on every later message.

  • compact_session_via_llm called session_save as soon as the summary model returned. The follow-up chat had not run yet. A timeout, 429, or 5xx after that summary left the raw messages replaced by the summary, while the failed turn itself was not stored.
  • The summary was always inserted as a new user message. With an odd max_context_messages (config allows 1–1000; example provider is Anthropic), the kept tail also starts on a user turn. The Messages API requires alternating roles and returns HTTP 400. Because the bad transcript was already saved, every later message on that session failed the same way until /reset.

The compacted JSON now stays in memory until agent_persist_session runs after a successful reply. If the kept tail already starts with user, the summary is folded into that turn instead of adding a second user message.

Testing

  • make test_agent then ./build/test_agent — passed, including:
    • test_compaction_failure_keeps_original_session (summary call succeeds, follow-up chat fails, msg_0 remains and the summary is not stored)
    • test_compaction_merges_summary_when_tail_starts_with_user (max_context_messages = 5, roles alternate, summary is inside the leading user turn, success still persists compaction)
    • existing test_context_compaction_when_history_exceeds_max
Open in Web View Automation 

Compaction wrote the summarized transcript before the reply, so a failed
provider call replaced the stored session with a summary the user never
accepted. When the kept tail already starts with a user turn, fold the
summary into that turn so Anthropic does not reject the session for
consecutive user roles.

Co-authored-by: esadrianno <esadrianno@gmail.com>
adriannoes added a commit that referenced this pull request Oct 17, 2026
Compaction wrote the summarized transcript before the reply, so a failed
provider call replaced the stored session with a summary the user never
accepted. When the kept tail already starts with a user turn, fold the
summary into that turn so Anthropic does not reject the session for
consecutive user roles.

Supersedes #126.
@adriannoes

Copy link
Copy Markdown
Collaborator

Superseded by #121: consolidated into the operator-authored branch fix/agent-summary-compaction (same fix, re-validated with make test_agent, CI=true make test, make static). Closing in favor of the consolidation.

@adriannoes adriannoes closed this Oct 17, 2026
@adriannoes
adriannoes deleted the cursor/high-severity-issues-9b09 branch October 17, 2026 05:29
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.

2 participants