fix(agents): enforce single-writer session files

This commit is contained in:
Peter Steinberger
2026-01-11 02:24:25 +00:00
parent 3a113b7752
commit 6668805aca
4 changed files with 220 additions and 69 deletions

View File

@@ -90,11 +90,10 @@ export function sanitizeToolUseResultPairing(
const role = (msg as { role?: unknown }).role;
if (role !== "assistant") {
if (role === "toolResult") {
pushToolResult(msg as Extract<AgentMessage, { role: "toolResult" }>);
} else {
out.push(msg);
}
// Tool results must only appear directly after the matching assistant tool call turn.
// Any "free-floating" toolResult entries in session history can make strict providers
// (Anthropic-compatible APIs, MiniMax, Cloud Code Assist) reject the entire request.
if (role !== "toolResult") out.push(msg);
continue;
}
@@ -141,7 +140,8 @@ export function sanitizeToolUseResultPairing(
}
}
remainder.push(next);
// Drop tool results that don't match the current assistant tool calls.
if (nextRole !== "toolResult") remainder.push(next);
}
out.push(msg);
@@ -159,11 +159,6 @@ export function sanitizeToolUseResultPairing(
out.push(rem);
continue;
}
const remRole = (rem as { role?: unknown }).role;
if (remRole === "toolResult") {
pushToolResult(rem as Extract<AgentMessage, { role: "toolResult" }>);
continue;
}
out.push(rem);
}
i = j - 1;