Skip to content

fix: preserve ToolPart structure during pruning to prevent malformed request errors#23

Merged
tuanhung303 merged 1 commit intomasterfrom
fix/malformed-request-toolpart-structure
Feb 13, 2026
Merged

fix: preserve ToolPart structure during pruning to prevent malformed request errors#23
tuanhung303 merged 1 commit intomasterfrom
fix/malformed-request-toolpart-structure

Conversation

@tuanhung303
Copy link
Owner

Summary

  • Root cause: ACP replaced pruned ToolParts with TextParts, breaking the tool-call/tool-result pairing that Anthropic's API requires. At high context usage this caused Improperly formed request. (reason: None) errors that locked the session.
  • Fix: Prune only replaces state.output on the ToolPart instead of swapping the entire part type. Also adds an empty-parts safety guard in filterStepMarkers.
  • Bonus: Context pressure now uses provider-reported token counts (msg.info.tokens) instead of heuristic estimates, closing the ~30-40% undercount gap.

Changes

File Change
lib/messages/prune.ts Keep ToolPart structure, replace only state.output; empty-parts guard in filterStepMarkers
lib/strategies/utils.ts Add getRealTokenCount() reading real tokens from last assistant message
lib/hooks.ts Use real tokens in computeContextPressure with heuristic fallback
tests/messages/prune.test.ts Updated existing tests + new filterStepMarkers tests
tests/strategies/utils.test.ts New tests for getRealTokenCount()

Testing

  • 580 tests passing, 0 failures
  • Clean build (tsc no errors)

…request errors

ACP was replacing pruned ToolParts with TextParts, breaking the tool-call/tool-result
pairing that Anthropic's API requires. This caused 'Improperly formed request' errors
at high context usage. Now prune only replaces state.output while keeping the ToolPart
intact. Also adds empty-parts guard in filterStepMarkers and uses provider-reported
token counts for accurate context pressure measurement.
@tuanhung303 tuanhung303 merged commit e0d5121 into master Feb 13, 2026
3 checks passed
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