Skip to content
This repository was archived by the owner on May 15, 2026. It is now read-only.

Commit b1edf1d

Browse files
committed
fix: convert orphaned tool_results to text blocks after condensing
When condensing occurs after assistant sends tool_uses but before user responds, the tool_use blocks get condensed away. User messages containing tool_results that reference condensed tool_use_ids become orphaned and get filtered out by getEffectiveApiHistory, causing user feedback to be lost. This fix enhances the existing check in addToApiConversationHistory to detect when the previous effective message is not an assistant and converts any tool_result blocks to text blocks, preventing them from being filtered as orphans. The conversion happens at the latest possible moment (message insertion) because: - Tool results are created before we know if condensing will occur - We need actual effective history state to make the decision - This is the last checkpoint before orphan filtering happens
1 parent f7434de commit b1edf1d

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

src/core/task/Task.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,27 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
10171017
const effectiveHistoryForValidation = getEffectiveApiHistory(this.apiConversationHistory)
10181018
const lastEffective = effectiveHistoryForValidation[effectiveHistoryForValidation.length - 1]
10191019
const historyForValidation = lastEffective?.role === "assistant" ? effectiveHistoryForValidation : []
1020-
const validatedMessage = validateAndFixToolResultIds(message, historyForValidation)
1020+
1021+
// If the previous effective message is NOT an assistant, convert tool_result blocks to text blocks.
1022+
// This prevents orphaned tool_results from being filtered out by getEffectiveApiHistory.
1023+
// This can happen when condensing occurs after the assistant sends tool_uses but before
1024+
// the user responds - the tool_use blocks get condensed away, leaving orphaned tool_results.
1025+
let messageToAdd = message
1026+
if (lastEffective?.role !== "assistant" && Array.isArray(message.content)) {
1027+
messageToAdd = {
1028+
...message,
1029+
content: message.content.map((block) =>
1030+
block.type === "tool_result"
1031+
? {
1032+
type: "text" as const,
1033+
text: `Tool result:\n${typeof block.content === "string" ? block.content : JSON.stringify(block.content)}`,
1034+
}
1035+
: block,
1036+
),
1037+
}
1038+
}
1039+
1040+
const validatedMessage = validateAndFixToolResultIds(messageToAdd, historyForValidation)
10211041
const messageWithTs = { ...validatedMessage, ts: Date.now() }
10221042
this.apiConversationHistory.push(messageWithTs)
10231043
}

0 commit comments

Comments
 (0)