mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-30 02:22:25 +00:00
fix: finalize safer MCP tool naming (#49505) (thanks @ziomancer)
This commit is contained in:
@@ -762,7 +762,7 @@ export async function compactEmbeddedPiSessionDirect(
|
||||
// limitHistoryTurns can orphan tool_result blocks by removing the
|
||||
// assistant message that contained the matching tool_use.
|
||||
const limited = transcriptPolicy.repairToolUseResultPairing
|
||||
? sanitizeToolUseResultPairing(truncated)
|
||||
? sanitizeToolUseResultPairing(truncated, { dropErroredAssistantResults: true })
|
||||
: truncated;
|
||||
if (limited.length > 0) {
|
||||
session.agent.replaceMessages(limited);
|
||||
|
||||
@@ -617,7 +617,7 @@ export async function sanitizeSessionHistory(params: {
|
||||
allowedToolNames: params.allowedToolNames,
|
||||
});
|
||||
const repairedTools = policy.repairToolUseResultPairing
|
||||
? sanitizeToolUseResultPairing(sanitizedToolCalls)
|
||||
? sanitizeToolUseResultPairing(sanitizedToolCalls, { dropErroredAssistantResults: true })
|
||||
: sanitizedToolCalls;
|
||||
const sanitizedToolResults = stripToolResultDetails(repairedTools);
|
||||
const sanitizedCompactionUsage = ensureAssistantUsageSnapshots(
|
||||
|
||||
@@ -565,9 +565,7 @@ export function wrapStreamFnSanitizeMalformedToolCalls(
|
||||
if (sanitized.messages === messages) {
|
||||
return baseFn(model, context, options);
|
||||
}
|
||||
let nextMessages = sanitizeToolUseResultPairing(sanitized.messages, {
|
||||
preserveErroredAssistantResults: true,
|
||||
});
|
||||
let nextMessages = sanitizeToolUseResultPairing(sanitized.messages);
|
||||
if (transcriptPolicy?.validateAnthropicTurns) {
|
||||
nextMessages = sanitizeAnthropicReplayToolResults(nextMessages);
|
||||
}
|
||||
|
||||
@@ -1109,7 +1109,7 @@ export async function runEmbeddedAttempt(
|
||||
// limitHistoryTurns can orphan tool_result blocks by removing the
|
||||
// assistant message that contained the matching tool_use.
|
||||
const limited = transcriptPolicy.repairToolUseResultPairing
|
||||
? sanitizeToolUseResultPairing(truncated)
|
||||
? sanitizeToolUseResultPairing(truncated, { dropErroredAssistantResults: true })
|
||||
: truncated;
|
||||
cacheTrace?.recordStage("session:limited", { messages: limited });
|
||||
if (limited.length > 0) {
|
||||
|
||||
@@ -235,6 +235,32 @@ describe("sanitizeToolUseResultPairing", () => {
|
||||
expect(result.messages[2]?.role).toBe("user");
|
||||
expect(result.added).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("drops matching tool results for aborted assistant messages when requested", () => {
|
||||
const input = castAgentMessages([
|
||||
{
|
||||
role: "assistant",
|
||||
content: [{ type: "toolCall", id: "call_aborted", name: "exec", arguments: {} }],
|
||||
stopReason: "aborted",
|
||||
},
|
||||
{
|
||||
role: "toolResult",
|
||||
toolCallId: "call_aborted",
|
||||
toolName: "exec",
|
||||
content: [{ type: "text", text: "partial result" }],
|
||||
isError: false,
|
||||
},
|
||||
{ role: "user", content: "retrying" },
|
||||
]);
|
||||
|
||||
const result = repairToolUseResultPairing(input, { dropErroredAssistantResults: true });
|
||||
|
||||
expect(result.droppedOrphanCount).toBe(0);
|
||||
expect(result.messages).toHaveLength(2);
|
||||
expect(result.messages[0]?.role).toBe("assistant");
|
||||
expect(result.messages[1]?.role).toBe("user");
|
||||
expect(result.added).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("sanitizeToolCallInputs", () => {
|
||||
|
||||
@@ -196,7 +196,7 @@ export type ToolCallInputRepairOptions = {
|
||||
};
|
||||
|
||||
export type ToolUseResultPairingOptions = {
|
||||
preserveErroredAssistantResults?: boolean;
|
||||
dropErroredAssistantResults?: boolean;
|
||||
};
|
||||
|
||||
export function stripToolResultDetails(messages: AgentMessage[]): AgentMessage[] {
|
||||
@@ -463,7 +463,7 @@ export function repairToolUseResultPairing(
|
||||
const stopReason = (assistant as { stopReason?: string }).stopReason;
|
||||
if (stopReason === "error" || stopReason === "aborted") {
|
||||
out.push(msg);
|
||||
if (options?.preserveErroredAssistantResults) {
|
||||
if (!options?.dropErroredAssistantResults) {
|
||||
for (const toolCall of toolCalls) {
|
||||
const result = spanResultsById.get(toolCall.id);
|
||||
if (!result) {
|
||||
@@ -471,6 +471,8 @@ export function repairToolUseResultPairing(
|
||||
}
|
||||
pushToolResult(result);
|
||||
}
|
||||
} else if (spanResultsById.size > 0) {
|
||||
changed = true;
|
||||
}
|
||||
for (const rem of remainder) {
|
||||
out.push(rem);
|
||||
|
||||
Reference in New Issue
Block a user