openclaw - ✅(Solved) Fix stripDanglingAnthropicToolUses misses tool_use blocks when no user turn follows [1 pull requests, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#56757Fetched 2026-04-08 01:48:09
View on GitHub
Comments
0
Participants
1
Timeline
1
Reactions
0
Participants
Timeline (top)
cross-referenced ×1

Error Message

  1. Network timeout / transient error causes the tool_result to never be delivered

Root Cause

// turns.ts — stripDanglingAnthropicToolUses
if ((nextMsg?.role) !== "user") {
    result.push(msg);   // ← tool_use blocks pass through unfiltered
    continue;
}

When nextMsg is undefined (assistant is last message) or when the next message has a different role, the function skips filtering entirely. It should also strip tool_use blocks in this case, since there is provably no matching tool_result.

Fix Action

Fix / Workaround

  • Users hit this on transient network issues — the session becomes permanently broken
  • Only workaround is /new (losing all conversation context)
  • Especially painful in long sessions with accumulated context

PR fix notes

PR #56803: fix: strip terminal dangling anthropic tool uses

Description (problem / solution / changelog)

Summary

  • strip dangling Anthropic toolUse blocks when the assistant message is the terminal turn in history
  • preserve the existing behavior for non-terminal assistant messages followed by another assistant/system turn
  • add regression coverage for terminal dangling-tool cases, including fallback placeholder behavior

Testing

  • pnpm exec vitest run src/agents/pi-embedded-helpers.validate-turns.test.ts

Closes #56757

Changed files

  • src/agents/pi-embedded-helpers.validate-turns.test.ts (modified, +37/-3)
  • src/agents/pi-embedded-helpers/turns.ts (modified, +8/-4)

Code Example

// turns.ts — stripDanglingAnthropicToolUses
if ((nextMsg?.role) !== "user") {
    result.push(msg);   // ← tool_use blocks pass through unfiltered
    continue;
}
RAW_BUFFERClick to expand / collapse

Bug

stripDanglingAnthropicToolUses() in src/agents/pi-embedded-helpers/turns.ts does not strip tool_use blocks from the last assistant message when no subsequent user message exists.

Steps to Reproduce

  1. Agent makes a tool call (assistant message with tool_use block)
  2. Network timeout / transient error causes the tool_result to never be delivered
  3. User sends a new message → this becomes the next user turn, but it does not contain a tool_result block for the previous tool_use
  4. stripDanglingAnthropicToolUses() only checks for orphaned tool_use when nextMsg.role === "user" — if the assistant message is the last in the array (no next message at all), the early-return at the !== "user" check lets the tool_use blocks through unfiltered
  5. Anthropic API rejects: "tool_use ids were found without tool_result blocks immediately after"
  6. Session is unrecoverable — user must /new

Root Cause

// turns.ts — stripDanglingAnthropicToolUses
if ((nextMsg?.role) !== "user") {
    result.push(msg);   // ← tool_use blocks pass through unfiltered
    continue;
}

When nextMsg is undefined (assistant is last message) or when the next message has a different role, the function skips filtering entirely. It should also strip tool_use blocks in this case, since there is provably no matching tool_result.

Suggested Fix

When the branch nextMsg.role !== "user" is taken and the assistant content contains tool_use blocks, filter them out (same logic as the existing dangling-removal path). Replace with [tool calls omitted] text block if all content was tool_use.

Impact

  • Users hit this on transient network issues — the session becomes permanently broken
  • Only workaround is /new (losing all conversation context)
  • Especially painful in long sessions with accumulated context

Environment

  • OpenClaw 2026.3.24
  • Provider: Anthropic (Claude Sonnet/Opus)
  • macOS, arm64

extent analysis

Fix Plan

To fix the issue, we need to modify the stripDanglingAnthropicToolUses() function to handle the case when the assistant message is the last in the array. We will add a check to filter out tool_use blocks when there is no subsequent user message.

Code Changes

// turns.ts — stripDanglingAnthropicToolUses
if ((nextMsg?.role) !== "user") {
    // Check if the current message contains tool_use blocks
    if (msg.content.includes("tool_use")) {
        // Filter out tool_use blocks
        const filteredContent = msg.content.replace(/tool_use.*?\/tool_use/g, '');
        if (filteredContent === '') {
            // If all content was tool_use, replace with [tool calls omitted] text block
            result.push({ content: '[tool calls omitted]', role: msg.role });
        } else {
            result.push({ content: filteredContent, role: msg.role });
        }
    } else {
        result.push(msg);
    }
    continue;
}

Verification

To verify that the fix worked, you can follow these steps:

  • Reproduce the issue by simulating a network timeout or transient error after an agent makes a tool call.
  • Check if the stripDanglingAnthropicToolUses() function correctly filters out tool_use blocks from the last assistant message.
  • Verify that the Anthropic API no longer rejects the session with the error message "tool_use ids were found without tool_result blocks immediately after".
  • Test the fix in different scenarios, including long sessions with accumulated context.

Extra Tips

  • Make sure to test the fix thoroughly to ensure that it does not introduce any new issues.
  • Consider adding additional logging or monitoring to detect and handle similar issues in the future.
  • Review the code changes to ensure that they align with the project's coding standards and best practices.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING