Skip to content

Conversation

@daniel-lxs
Copy link
Member

@daniel-lxs daniel-lxs commented Jun 24, 2025

Problem

Long Claude code messages were being truncated or causing parsing failures because JSON chunks could be split across multiple data events, leading to incomplete JSON parsing errors.

Solution

Implements improvements from Cline PR #4287 by @BarreiroT to fix handling of incomplete messages when using Claude Code.

Key Changes

  • Async Generator Pattern: Replaced event-driven process handling with clean async generator using readline interface
  • ProcessState Management: Introduced centralized state tracking for partial data, errors, and exit codes
  • Improved Chunk Parsing: Intelligent handling of incomplete JSON chunks via partialData accumulation
  • Enhanced Error Handling: Better API error parsing and subscription vs paid usage detection
  • Extended Content Support: Added support for thinking, redacted_thinking, and tool_use content types
  • Message Filtering: Filters out unsupported image blocks with descriptive placeholders
  • Enhanced Configuration: Added --disallowedTools, increased output token limits, and improved timeouts
  • Model Configuration: Explicitly disabled unsupported features (images, prompt cache) for Claude Code models

Test Coverage

Added comprehensive test suite with 20 tests:

  • Claude Code handler tests (10 tests)
  • Message filter tests (8 tests)
  • Run module tests (2 tests)

Files Changed

  • src/api/providers/claude-code.ts - Main handler with async generator pattern
  • src/integrations/claude-code/run.ts - Process management with ProcessState
  • src/integrations/claude-code/types.ts - Updated type definitions
  • src/integrations/claude-code/message-filter.ts - New image filtering utility
  • packages/types/src/providers/claude-code.ts - Model configuration updates
  • Test files for comprehensive coverage

Credits

Based on Cline PR #4287 "fix: Handle long Claude code messages" by @BarreiroT

Testing

✅ All 20 tests passing
✅ Type checking passed
✅ Linting passed

Fixes issues with long-lived, complex tasks that generate longer outputs from Claude Code CLI.


Important

Fixes handling of long Claude code messages by implementing async generator pattern, improving chunk parsing, and enhancing error handling.

  • Behavior:
    • Replaces event-driven process handling with async generator in claude-code.ts.
    • Improves JSON chunk parsing and error handling in run.ts.
    • Filters unsupported image blocks in message-filter.ts.
    • Adds support for thinking, redacted_thinking, and tool_use content types.
    • Enhances configuration with --disallowedTools and increased output token limits.
  • Models:
    • Updates claudeCodeModels in claude-code.ts to disable unsupported features for Claude Code models.
  • Tests:
    • Adds 20 tests covering Claude Code handler, message filtering, and run module.
  • Misc:
    • Refactors runClaudeCode to use async generator pattern in run.ts.
    • Updates type definitions in types.ts.

This description was created by Ellipsis for 097dbd3. You can customize this summary. It will automatically update as commits are pushed.

Implements async generator pattern and ProcessState management to properly
handle incomplete JSON chunks from Claude Code CLI, preventing parsing
failures with long outputs.

Based on Cline PR #4287 by @BarreiroT
@daniel-lxs daniel-lxs requested review from cte, jr and mrubens as code owners June 24, 2025 14:55
@dosubot dosubot bot added the size:XXL This PR changes 1000+ lines, ignoring generated files. label Jun 24, 2025
@delve-auditor
Copy link

delve-auditor bot commented Jun 24, 2025

No security or compliance issues detected. Reviewed everything up to 097dbd3.

Security Overview
  • 🔎 Scanned files: 9 changed file(s)
Detected Code Changes
Change Type Relevant files
Bug Fix ► registerCommands.spec.ts
    Add workspace.workspaceFolders mock to VSCode mock
Enhancement ► claude-code.ts
    Update message parsing and filtering logic
► types.ts
    Update Claude Code message types
► run.ts
    Improve Claude Code process handling
► message-filter.ts
    Add message filtering for Claude Code
Refactor ► claude-code.spec.ts
    Update and expand test coverage

Reply to this PR with @delve-auditor followed by a description of what change you want and we'll auto-submit a change to this PR to implement it.

@daniel-lxs daniel-lxs moved this from Triage to PR [Needs Review] in Roo Code Roadmap Jun 24, 2025
@daniel-lxs
Copy link
Member Author

Fixing the unit test...

- Add workspace.workspaceFolders mock to VSCode mock in registerCommands.spec.ts
- Fixes test failure where claude-code modules access vscode.workspace at module level
- Resolves 'No workspace export is defined on the vscode mock' error
@dosubot dosubot bot added the bug Something isn't working label Jun 24, 2025
@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Jun 24, 2025
@mrubens mrubens merged commit 954825a into main Jun 24, 2025
14 checks passed
@mrubens mrubens deleted the fix/claude-code-long-messages branch June 24, 2025 15:34
@github-project-automation github-project-automation bot moved this from PR [Needs Review] to Done in Roo Code Roadmap Jun 24, 2025
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Jun 24, 2025
cte pushed a commit that referenced this pull request Jun 24, 2025
Alorse pushed a commit to Alorse/Roo-Code that referenced this pull request Jun 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working lgtm This PR has been approved by a maintainer PR - Needs Review size:XXL This PR changes 1000+ lines, ignoring generated files.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

4 participants