Skip to content

fix(compressor): summary role can violate consecutive-role constraint#1720

Merged
teknium1 merged 1 commit intomainfrom
fix/compressor-consecutive-role-violation
Mar 17, 2026
Merged

fix(compressor): summary role can violate consecutive-role constraint#1720
teknium1 merged 1 commit intomainfrom
fix/compressor-consecutive-role-violation

Conversation

@teknium1
Copy link
Copy Markdown
Contributor

Summary

The context compressor's summary message role was determined only by the last head message's role, ignoring what the first tail message's role is. When the last head message was assistant and the first tail message was user, the summary role was set to user — creating consecutive user messages that Anthropic's API rejects.

What changed

  • agent/context_compressor.py: Now checks both the head and tail neighbors when choosing the summary role. Prioritizes not colliding with the head (already committed), then avoids the tail collision if possible without re-colliding with the head.

Test plan

  • python -m pytest tests/ -n0 -q -k compress → 61 passed, 14 skipped ✔
  • Existing test test_summary_role_avoids_consecutive_user_messages still passes
  • Edge case: when both neighbors conflict (impossible to avoid both), prefers head-safe
The summary message role was determined only by the last head message,
ignoring the first tail message. This could create consecutive user
messages (rejected by Anthropic) when the tail started with 'user'.

Now checks both neighbors. Priority: avoid colliding with the head
(already committed). If the chosen role also collides with the tail,
flip it — but only if flipping wouldn't re-collide with the head.
@teknium1 teknium1 merged commit ec272ca into main Mar 17, 2026
1 check passed
teknium1 added a commit that referenced this pull request Mar 30, 2026
…, tool error sanitization

Three resilience features ported from Ironclaw:

1. Discard incomplete tool calls (ironclaw#1632)
   When finish_reason='length' and tool calls are present, they're likely
   incomplete. Discard them, inject a summarize notice. After 3 consecutive
   occurrences, temporarily disable tools.

2. Empty response recovery (ironclaw#1677 + #1720)
   When the LLM returns empty (no content, no tool calls):
   - If meaningful output exists earlier, treat as completion
   - Otherwise nudge once, then fail gracefully
   Max 2 consecutive empties before giving up.

3. Sanitize tool error results (ironclaw#1639)
   Strip XML boundary markers, CDATA sections, and code fences from error
   messages before sending to LLM. Cap at 2000 chars. Prevents
   injection attacks via crafted tool error messages.

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

Labels

None yet

1 participant