fix(core): keep V1 tool calls that never recorded a run time

This commit is contained in:
Aiden Cline
2026-11-03 15:39:08 -06:00
Unverified
parent 56c33e146f
commit 53779fd4d3
2 changed files with 52 additions and 10 deletions
@@ -139,28 +139,32 @@ const toolResult = (tool: SessionMessage.AssistantTool, providerMetadata: Provid
}
}
// A call cut off before it ran (by Esc, a crash or a failed stream) is not replayed. Input
// errors stay so the model can reissue the call.
const isAbandonedTool = (item: SessionMessage.AssistantContent) =>
item.type === "tool" &&
item.time.ran === undefined &&
!(item.state.status === "error" && item.state.error.type.startsWith("tool.input"))
// Sessions migrated from V1 never recorded `time.ran`, so a completed call also counts as run.
const ranTool = (item: SessionMessage.AssistantContent) =>
item.type === "tool" && (item.time.ran !== undefined || item.state.status === "completed")
const assistant = (message: SessionMessage.Assistant, model: Model.Ref, providerMetadataKey: string) => {
const sameProvider = message.model.providerID === model.providerID
const sameModel = sameProvider && message.model.id === model.id
// A failed stream is retried from what had already happened: the calls that ran and everything before
// them. The rest was cut off, and dropping it keeps the request from ending in a partial reply.
const ran = message.content.findLastIndex((item) => item.type === "tool" && item.time.ran !== undefined)
const kept =
message.error === undefined || message.error.type === "aborted"
? message.content
: message.content.slice(0, ran + 1)
const items = kept.filter((item) => !isAbandonedTool(item))
: message.content.slice(0, message.content.findLastIndex(ranTool) + 1)
// A stream cut by Esc, a crash or a failure may end mid-block, so its last block's provider state is not
// replayed, and reasoning is only replayed once the model moved on to output.
// replayed, and reasoning is only replayed once the model moved on to output. Calls it cut off before
// they ran are dropped; input errors stay so the model can reissue the call.
const cut = message.error !== undefined || message.finish === undefined
const tail = cut ? message.content.at(-1) : undefined
const items = cut
? kept.filter(
(item) =>
item.type !== "tool" ||
ranTool(item) ||
(item.state.status === "error" && item.state.error.type.startsWith("tool.input")),
)
: kept
const content = items.flatMap((item, index): Array<AssistantMessage["content"][number]> => {
if (item.type === "text")
return [
@@ -806,6 +806,7 @@ Earlier work
expect(messages[0]?.content).toEqual([
{ type: "text", text: "Checking" },
{ type: "reasoning", text: "Think", providerMetadata: { provider: { signature: "sig_1" } } },
{ type: "tool-call", id: "pending", name: "read", input: { path: "README.md" } },
{ type: "tool-call", id: "running", name: "read", input: { path: "README.md" } },
{
type: "tool-call",
@@ -1507,6 +1508,43 @@ Earlier work
expect(failedWithCalls[0]?.content[1]).toMatchObject({ type: "tool-call", id: "malformed" })
})
test("replays tools migrated from V1, which never recorded when they ran", () => {
const migrated = (value: string, error?: SessionMessage.Assistant["error"]) =>
SessionMessage.Assistant.make({
id: id(value),
type: "assistant",
agent: build,
model: { id: Model.ID.make("same"), providerID: Provider.ID.make("provider") },
content: [
SessionMessage.AssistantTool.make({
type: "tool",
id: `${value}-read`,
name: "read",
state: SessionMessage.ToolStateCompleted.make({
status: "completed",
input: { path: "README.md" },
content: [{ type: "text", text: "Hello" }],
}),
time: { created, completed: created },
}),
],
error,
finish: error === undefined ? "tool-calls" : "error",
time: { created, completed: created },
})
const messages = toLLMMessages(
[
migrated("finished"),
migrated("aborted", { type: "aborted", message: "Step interrupted" }),
migrated("failed", { type: "provider.unknown", message: "Stream dropped" }),
],
Model.Ref.make({ id: Model.ID.make("same"), providerID: Provider.ID.make("provider") }),
)
expect(
messages.map((message) => message.content.map((part) => (part.type === "tool-call" ? part.id : part.type))),
).toEqual([["finished-read"], ["tool-result"], ["aborted-read"], ["tool-result"], ["failed-read"], ["tool-result"]])
})
test("preserves assistant text provider state for the same model", () => {
const messages = toLLMMessages(
[