Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

fix(claude): drop orphan tool_result after fixToolAdjacency strip (discussion #2410)

diegosouzapwfbf37ae0daf3
diff --git a/open-sse/executors/base.ts b/open-sse/executors/base.ts
index c91bc71e1f..7e656f311a 100644
--- a/open-sse/executors/base.ts
+++ b/open-sse/executors/base.ts
@@ -894,7 +894,10 @@ export class BaseExecutor {
// Only apply for Claude/Claude-compatible — OpenAI allows results
// spread across multiple subsequent messages.
const isClaude = this.provider === "claude" || isClaudeCodeCompatible(this.provider);
- const adjacent = isClaude ? fixToolAdjacency(fixed) : fixed;
+ // For Claude, fixToolAdjacency may strip tool_use blocks whose
+ // tool_result isn't in the next message; re-run fixToolPairs to
+ // drop any tool_result orphaned by that strip (discussion #2410).
+ const adjacent = isClaude ? fixToolPairs(fixToolAdjacency(fixed)) : fixed;
tb.messages = stripTrailingAssistantOrphanToolUse(adjacent);
}
}
diff --git a/open-sse/services/claudeCodeCompatible.ts b/open-sse/services/claudeCodeCompatible.ts
index 1e41b03843..6b7bd98b04 100644
--- a/open-sse/services/claudeCodeCompatible.ts
+++ b/open-sse/services/claudeCodeCompatible.ts
@@ -378,7 +378,11 @@ export async function buildAndSignClaudeCodeRequest(
if (Array.isArray(b.messages)) {
const fixed = fixToolPairs(b.messages as Record<string, unknown>[]);
const adjacent = fixToolAdjacency(fixed);
- b.messages = stripTrailingAssistantOrphanToolUse(adjacent);
+ // fixToolAdjacency can leave orphan tool_result blocks behind when it
+ // strips a tool_use whose tool_result wasn't in the next message.
+ // Re-pair to drop those orphans (discussion #2410).
+ const cleaned = fixToolPairs(adjacent);
+ b.messages = stripTrailingAssistantOrphanToolUse(cleaned);
}
}
@@ -1158,7 +1162,9 @@ function readNestedString(
if (!current || typeof current !== "object" || Array.isArray(current)) {
return null;
}
- current = (current as Record<string, unknown>)[key];
+ if (key === "__proto__" || key === "constructor" || key === "prototype") return null;
+ if (!Object.prototype.hasOwnProperty.call(current, key)) return null;
+ current = Reflect.get(current as object, key);
}
return toNonEmptyString(current);
}
diff --git a/open-sse/services/contextManager.ts b/open-sse/services/contextManager.ts
index 8c203ddf48..c31215c7d8 100644
--- a/open-sse/services/contextManager.ts
+++ b/open-sse/services/contextManager.ts
@@ -267,6 +267,9 @@ function purifyHistory(messages: Record<string, unknown>[], targetTokens: number
let candidate = [...system, ...nonSystem.slice(-keep)];
candidate = fixToolPairs(candidate);
candidate = fixToolAdjacency(candidate);
+ // Re-run pair fix: fixToolAdjacency may have stripped tool_use blocks, leaving
+ // orphan tool_results that Claude rejects ("tool_result without preceding tool_use").
+ candidate = fixToolPairs(candidate);
candidate = stripTrailingAssistantOrphanToolUse(candidate);
const tokens = estimateTokens(JSON.stringify(candidate));
if (tokens <= targetTokens) break;
@@ -276,6 +279,9 @@ function purifyHistory(messages: Record<string, unknown>[], targetTokens: number
let result = [...system, ...nonSystem.slice(-keep)];
result = fixToolPairs(result);
result = fixToolAdjacency(result);
+ // Re-run pair fix to drop any tool_result whose matching tool_use was removed by
+ // fixToolAdjacency (discussion #2410 — orphan tool_result -> upstream 400).
+ result = fixToolPairs(result);
result = stripTrailingAssistantOrphanToolUse(result);
// Add summary of dropped messages