Skip to content

fix(claudecode): handle mid-turn compaction events in readLoop#483

Open
chenhg5 wants to merge 1 commit intomainfrom
fix/claudecode-midturn-compaction-issue-481
Open

fix(claudecode): handle mid-turn compaction events in readLoop#483
chenhg5 wants to merge 1 commit intomainfrom
fix/claudecode-midturn-compaction-issue-481

Conversation

@chenhg5
Copy link
Copy Markdown
Owner

@chenhg5 chenhg5 commented Apr 7, 2026

Summary

  • Add default case in event switch to log unknown event types for diagnostic purposes
  • Handle compaction result events by checking subtype field (compact or compaction)
  • Set Done=false for compaction result events so the event loop continues processing

Problem

When Claude Code performs automatic context compaction mid-turn, cc-connect could misinterpret compaction-related events as turn completion, causing the task to be interrupted prematurely. Unknown event types were silently dropped, making diagnosis difficult.

Solution

  1. Diagnostic logging: Unknown event types are now logged with their full content, helping identify new event types Claude Code may emit
  2. Compaction detection: Result events with subtype: "compact" or subtype: "compaction" are treated as intermediate events (Done=false), not turn completion

Test plan

  • go build ./agent/claudecode/... passes
  • go vet ./agent/claudecode/... passes
  • go test ./agent/claudecode/... passes
  • Unit tests for Done=true on normal result
  • Unit tests for Done=false on compaction subtypes

Closes #481

🤖 Generated with Claude Code

Add two improvements to handle Claude Code's context compaction events:

1. Add default case in event switch to log unknown event types for
   diagnostic purposes. Previously, unknown events were silently dropped.

2. Handle compaction result events by checking subtype field. When
   Claude Code emits a result event with subtype "compact" or
   "compaction", it indicates mid-turn context compression, not turn
   completion. These events now set Done=false so the event loop
   continues processing.

This prevents premature task interruption when Claude Code performs
automatic context compression during long turns.

Changes:
- Add default case with debug logging for unknown event types
- Add subtype detection in handleResult for compaction events
- Set Done=false for compaction result events
- Add tests for Done field behavior

Closes #481

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown
Owner Author

@chenhg5 chenhg5 left a comment

Choose a reason for hiding this comment

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

LGTM. Correctly handles compaction events by checking subtype field, preventing premature turn interruption. Good diagnostic logging for unknown events.

@chenhg5
Copy link
Copy Markdown
Owner Author

chenhg5 commented Apr 8, 2026

QA Review: ✅ LGTM

  • Adds logging for unknown event types (diagnostic aid)
  • Correctly distinguishes compaction result from turn completion
  • Done=false for compact/compaction subtype prevents early termination
  • Tests cover normal and compaction cases
  • CI passes

Ready for self-approval or external reviewer.

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.

bug: Claude Code context compaction mid-turn causes task interruption

2 participants