Skip to content

Context pruning can split tool_use/tool_result pairs, causing API validation errors #46319

Closed

Description

@fredhazelton

Bug

Context pruning can split tool_use/tool_result message pairs, causing Anthropic API validation errors.

Root Cause Analysis

After tracing the code in pi-embedded-runner/run/attempt.js (lines ~431–447), the message pipeline runs in this order:

  1. sanitizeSessionHistory() — calls repairToolUseResultPairing() which correctly fixes orphaned/displaced tool results
  2. validateGeminiTurns() / validateAnthropicTurns() — merges consecutive same-role turns
  3. limitHistoryTurns() — slices messages to keep only the last N user turns
  4. replaceMessages() — feeds the result to the agent
  5. Context pruning extension fires on the context event (soft-trims / hard-clears tool result content in-place, does NOT remove messages)

The primary issue: limitHistoryTurns() (step 3) slices the message array on user-turn boundaries after repair has already run. This slice can cut between an assistant tool_use message and its tool_result, orphaning one side. There is no repair step after limitHistoryTurns.

Note: The context pruning extension itself (pruner.js) is actually safe — it only replaces tool result content with placeholders/trimmed versions, never removes messages from the array. The transcript-sanitize extension also exists with repair logic, but doesn't appear to be registered in buildEmbeddedExtensionPaths().

Symptoms

  • Sessions crash with Anthropic API errors about unexpected tool_result blocks (no matching tool_use) or orphaned tool_use blocks without a corresponding tool_result
  • Affected sessions become unrecoverable without manual intervention (compaction or session reset)
  • Most likely to trigger in long-running DM sessions with dmHistoryLimit configured

Suggested Fixes

  1. Move repair after limitHistoryTurns: Run repairToolUseResultPairing() (or at minimum a lightweight orphan check) after limitHistoryTurns() slices the array — this is the simplest targeted fix.

  2. Make limitHistoryTurns tool-pair-aware: When counting user turns for the slice point, ensure the cut doesn't land between an assistant+toolCall and its toolResult. If it would, include the full tool exchange.

  3. Register transcript-sanitize extension: The transcript-sanitize.js extension already exists and runs repairToolUseResultPairing on the context event — registering it in buildEmbeddedExtensionPaths() would add a safety net that catches any orphans right before the API call.

Activity

  1. steipete commented on Apr 25, 2026

    @steipete
    Contributor

    Closing this as implemented after Codex review.

    Current main already applies a second tool-use/tool-result repair pass immediately after limitHistoryTurns(), which directly addresses the reported truncation bug. The shared repair helper also drops free-floating orphan toolResult entries, and the same post-truncation safeguard is present in both normal embedded runs and compaction flows.

    What I checked:

    So I’m closing this as already implemented rather than keeping a duplicate issue open.

    Review notes: reviewed against c13836804058; fix evidence: commit 564f820efa2b.

  2. locked as resolved and limited conversation to collaborators on Apr 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions