agent-native icon indicating copy to clipboard operation
agent-native copied to clipboard

Tool calls from previous turns are dropped silently, so the agent denies work it did (follow-up to #6825)

Open nabilkhan199999 opened this issue 1 month ago • 0 comments

Summary

Following #6825, 0.203.0 (#6835) started sending completed tool calls from earlier turns as structuredHistory. On 0.207.0 some or all of those calls are missing, with no omission note.

The cause is in the server's thread-snapshot merge: a tool call stored as completed is never updated, so the messageId the browser adds later (once the reply text starts) is discarded. After the run the browser reloads the thread, gets tool calls with no messageId, and messagesWithToolCallHistory skips them. The model then concludes it did not do work it actually did, and the chat suggests repeating it.

Environment

  • App: Flow (internal editorial app built on Agent-Native)
  • @agent-native/core: 0.207.0
  • @agent-native/agentkit: 0.207.0
  • Model: gpt-5.6-terra via ai-sdk:openai, effort high, Act mode
  • We use createAgentChatPlugin with the built-in agent-native chat transport and the framework's chat UI.
  • We do not use a custom chat engine or history serializer.
  • No page reload between messages.

Steps and observed behavior

Run 1, new chat (partial loss):

  1. Ask: "You write me a skill that gives me three headline options for a certain body of text or for a certain subject." The agent makes four resources calls, all successful: read skills/write-skill/SKILL.md, list skills/, write skills/three-headline-options/SKILL.md ("Wrote resource … (1017 bytes)"), and read of the new skill. It replies: "Created and saved the personal three-headline-options skill…"
  2. Ask: "Tell me exactly what resources calls you made here or what tool calls you made in your last response".
  3. The follow-up request's structuredHistory contains only the fourth call (the read-back). The first three, including the write, are missing, with no omission note.
  4. The agent replies: "I made one tool call: … I did not create, write, save, or modify any resource—so my previous statement that I had 'created and saved' it was incorrect." A "Save the skill now" suggestion is shown.

Run 2, new chat (total loss), captured as a HAR:

  1. Ask: "Can you update my three headline option skills to be much more concise in its description?" The agent makes four resources calls (read write-skill, read the skill, write it, read it back) before writing any text, then replies: "Updated and verified the personal three-headline-options skill…"
  2. Ask: "what tool calls exactly did u make to find out". The request has no structuredHistory field and no omission note.
  3. The agent replies: "None in the recorded turn—the earlier reply was incorrect. I did not call resources…". An "Update the skill now" suggestion is shown.

What the HAR shows (run 2)

Time (UTC) Request Tool calls in it
14:21:12 PUT /agent-chat/threads/:id (mid-run snapshot delta) Calls 1–2, status: "completed", no messageId (no assistant message existed yet)
14:21:18 PUT …/threads/:id (mid-run) Calls 3–4, completed, no messageId
14:21:20 PUT …/threads/:id ×3 (after the reply) All four, completed, messageId: "message-ffb19f57-…" (the reply)
14:21:20–14:21:29 GET …/threads/:id ×4 All four, completed, no messageId
14:21:29 POST /agent-chat (follow-up) No structuredHistory

Cause

preferIncomingSnapshotEntry in packages/core/src/agent/thread-data-builder.ts only accepts an incoming tool call when the stored copy is still running:

  • if (kind === "toolCall") return existing.status === "running" && incoming.status !== "running";

When a model makes its tool calls before any reply text (common with OpenAI models), the browser saves those calls as completed with no messageId, because there is no assistant message to link them to yet (pendingToolMessageAssociations in agentkit-protocol.ts). When the reply starts, the browser re-saves them with the messageId. The server keeps the stored copy, so the link is lost. Run 1 fits this pattern: the last call was presumably still running at the last mid-run save, so its completed, linked copy replaced the stored one.

After the run, the browser reloads the thread and uses the server's tool calls. messagesWithToolCallHistory in packages/agentkit/src/client/client.ts keeps only calls whose messageId matches an assistant message (eligibleCalls). Unlinked calls are skipped without setting omittedHistory, so no omission note is added either.

Expected behavior

The next request should include every completed tool call and result from the previous turn, within the documented bounds (64 calls / 256 KiB), or an omission note when any are left out.

Impact

Follow-up questions about earlier actions, such as "did you save it?" or "what did you change?", get incorrect answers. In both runs the agent told the user a completed write had not happened, and the chat suggested doing it again. For tools with side effects, following that suggestion would repeat completed work.

Related behavior in 0.205.0

On 0.205.0 we also saw all tool history for a turn dropped with the note "Tool-call history was omitted because its position could not be reconstructed safely" (messagePartsWithToolHistory). That may be a separate path.

nabilkhan199999 avatar Oct 12 '26 15:10 nabilkhan199999