From 9da5aec96b83bba745d24b6cd6f591e5468c1c66 Mon Sep 17 00:00:00 2001 From: daniel-lxs Date: Tue, 16 Dec 2025 14:08:04 -0500 Subject: [PATCH] fix: validate tool_result IDs in delegation resume flow Add targeted validation at the injection site in reopenParentFromDelegation() to ensure tool_result blocks have matching tool_use IDs in the immediately preceding assistant message (Anthropic API requirement). Root cause: When parent task resumes after child delegation completes, a tool_result is injected with a tool_use_id found by searching backwards through the entire history. This ID may not exist in the immediately preceding assistant message, causing Anthropic to reject the request with 'unexpected tool_use_id found in tool_result blocks'. Fix: Call validateAndFixToolResultIds() on the newly injected user message before saving to disk. This validates/corrects tool_result IDs by position matching and filters orphaned tool_results. PostHog issue: https://us.posthog.com/error_tracking/019b27da-aa13-7670-906f-4bb5c3cd250e --- src/core/webview/ClineProvider.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/core/webview/ClineProvider.ts b/src/core/webview/ClineProvider.ts index deb7480056f..af7efaf11cc 100644 --- a/src/core/webview/ClineProvider.ts +++ b/src/core/webview/ClineProvider.ts @@ -98,6 +98,7 @@ import { readTaskMessages } from "../task-persistence/taskMessages" import { getNonce } from "./getNonce" import { getUri } from "./getUri" import { REQUESTY_BASE_URL } from "../../shared/utils/requesty" +import { validateAndFixToolResultIds } from "../task/validateToolResultIds" /** * https://github.com/microsoft/vscode-webview-ui-toolkit-samples/blob/main/default/weather-webview/src/providers/WeatherViewProvider.ts @@ -3167,6 +3168,15 @@ export class ClineProvider }) } + // Validate the newly injected tool_result against the preceding assistant message. + // This ensures the tool_result's tool_use_id matches a tool_use in the immediately + // preceding assistant message (Anthropic API requirement). + const lastMessage = parentApiMessages[parentApiMessages.length - 1] + if (lastMessage?.role === "user") { + const validatedMessage = validateAndFixToolResultIds(lastMessage, parentApiMessages.slice(0, -1)) + parentApiMessages[parentApiMessages.length - 1] = validatedMessage + } + await saveApiMessages({ messages: parentApiMessages as any, taskId: parentTaskId, globalStoragePath }) // 3) Update child metadata to "completed" status