Skip to content

Bug: transcript-sanitize extension never loaded — causes tool_use_id mismatch in long sessions #6118

@Dinpo

Description

@Dinpo

Bug: transcript-sanitize extension never loaded — causes tool_use_id mismatch in long sessions

Error Message

FailoverError: LLM request rejected: messages.268.content.1: unexpected `tool_use_id` found in `tool_result` blocks: toolu_01YDDkR7xihJHDSiQdF8VyzV. Each `tool_result` block must have a corresponding `tool_use` block in the previous message.

Also appears as:

Embedded agent failed before reply: LLM request rejected: messages.268.content.1: unexpected `tool_use_id` found in `tool_result` blocks

Environment

  • Clawdbot gateway with Anthropic provider (Claude)
  • Long-running session (268+ messages)

Root Cause

The transcript-sanitize extension (dist/agents/pi-extensions/transcript-sanitize.js) exists and is designed to run repairToolUseResultPairing() on every context build via the context event hook. However, buildEmbeddedExtensionPaths() in dist/agents/pi-embedded-runner/extensions.js never includes it in the extension paths.

This means:

  • repairToolUseResultPairing() runs once at session load (via sanitizeSessionHistory())
  • But it does not run on subsequent context builds during the same session
  • In long sessions, compaction, context pruning, and new message appends can cause tool_result blocks to become orphaned (referencing tool_use IDs that no longer exist in the preceding assistant message)
  • When the Anthropic API receives these orphaned references, it rejects the entire request

Proposed Fix

In src/agents/pi-embedded-runner/extensions.ts, add transcript-sanitize to buildEmbeddedExtensionPaths():

export function buildEmbeddedExtensionPaths(params) {
    const paths = [];
    // Always include transcript-sanitize: repairs tool_use/tool_result pairing
    // on every context build, not just at session load.
    paths.push(resolvePiExtensionPath("transcript-sanitize"));
    if (resolveCompactionMode(params.cfg) === "safeguard") {
        paths.push(resolvePiExtensionPath("compaction-safeguard"));
    }
    const pruning = buildContextPruningExtension(params);
    if (pruning.additionalExtensionPaths) {
        paths.push(...pruning.additionalExtensionPaths);
    }
    return paths;
}

Workaround

Patch dist/agents/pi-embedded-runner/extensions.js directly with the above change and restart the gateway. Note: this gets overwritten on update.

Searchable Keywords

unexpected tool_use_id, tool_result blocks, Each tool_result block must have a corresponding tool_use block, FailoverError, LLM request rejected, transcript-sanitize, repairToolUseResultPairing

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingstaleMarked as stale due to inactivity

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions