Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@spiceoogway
Copy link

Summary

Fixes #4367 - tool_use_id mismatch error after limitHistoryTurns truncation

Problem

The message processing pipeline had a synchronization bug:

  1. sanitizeSessionHistory() calls repairToolUseResultPairing() to fix tool_use/tool_result pairings
  2. limitHistoryTurns() runs AFTER the repair, truncating conversation history
  3. The truncation can cut between an assistant message (with tool_use) and its tool_result, creating orphaned tool_result blocks
  4. The Anthropic API rejects the malformed transcript with: unexpected tool_use_id found in tool_result blocks

Solution

Call sanitizeToolUseResultPairing() AFTER limitHistoryTurns() to repair any pairings broken by truncation.

Changes

  • Added import for sanitizeToolUseResultPairing from session-transcript-repair.js
  • Call sanitizeToolUseResultPairing() on the limited message array
  • Updated variable name from limited to repaired for clarity

Testing

  • Linter passed (pnpm lint)
  • Formatter passed (pnpm format)
  • Build succeeded (npm run build)

Files Modified

  • src/agents/pi-embedded-runner/run/attempt.ts

@moltbot-barnacle moltbot-barnacle bot added the agents Agent runtime and tooling label Jan 30, 2026
…ixes openclaw#4367)

The message processing pipeline had a synchronization bug where
limitHistoryTurns() truncated conversation history AFTER
repairToolUseResultPairing() had already fixed tool_use/tool_result
pairings. This could split assistant messages (with tool_use) from
their corresponding tool_result blocks, creating orphaned tool_result
blocks that the Anthropic API rejects.

This fix calls sanitizeToolUseResultPairing() AFTER limitHistoryTurns()
to repair any pairings broken by truncation, ensuring the transcript
remains valid before being sent to the LLM API.

Changes:
- Added import for sanitizeToolUseResultPairing from session-transcript-repair.js
- Call sanitizeToolUseResultPairing() on the limited message array
- Updated variable name from 'limited' to 'repaired' for clarity
@spiceoogway spiceoogway force-pushed the fix/tool-use-id-mismatch-4367 branch from 480fd42 to 8324201 Compare January 30, 2026 05:59
@spiceoogway
Copy link
Author

Rebased on latest main to fix stale lockfile failures (moltbot→openclaw rename).

The remaining CI failures are pre-existing on main:

  • install-smoke — Docker smoke test infra issue
  • formatsrc/commands/onboard-helpers.ts formatting (exists on main)
  • testsrc/config/paths.test.ts:52 expects length 1 but gets 16 (exists on main)

None of these are caused by this PR's changes.

@moltbot-barnacle moltbot-barnacle bot added the commands Command implementations label Jan 30, 2026
…runcation

Add tests for PR openclaw#4387 covering three critical scenarios:
1. Orphaned tool_use (tool call without result after truncation) - verifies synthetic error result insertion
2. Orphaned tool_result (result without call after truncation) - verifies orphan dropping
3. Normal history (well-formed transcript) - verifies no-op behavior

Also tests edge cases:
- Multiple orphaned tool_use blocks
- Mixed scenario with some results present, some missing

All tests pass. Fixes openclaw#4367
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling commands Command implementations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: tool_use_id mismatch after limitHistoryTurns truncation

1 participant