Skip to content

Copilot workspace: a turn that calls a frontend tool is saved twice in the conversation #751

Closed

Description

@mattbrailsford

Context

Found while fixing #748 (PR #750). In a Copilot Workspace chat, when the agent calls a frontend tool such as get_page_info, the first run's messages are stored twice in the conversation's history. #748's notes guessed the duplicate came from the attachment thread id mismatch. With that fixed, the duplicate is still there, so it has a different cause. When a chat is reopened it can show the turn twice, and the model reads the repeated turn on later runs.

Seen on the v18 demo site with #750 applied. Rows read from umbracoAIConversationsMessage for one chat (a user message with a PDF, then get_page_info, then a final reply, then a second plain turn):

seq stored message
0 user message (with the file)
1 assistant get_page_info call, informationalOnly: true, no arguments
2 tool result "Success: Function completed."
3 user message again, new message id
4 assistant get_page_info call again, informationalOnly: false, arguments: {}
5 the real tool result (url and pathname)
6 final assistant reply
7-8 second turn, not duplicated

So run 1 stores the turn with a placeholder tool result. Run 2 (the frontend tool's resume) then stores it again with the real result, under new message ids. DropAlreadyPersistedLeadingMessagesAsync doesn't drop the resent copy.

What we want

  • A frontend tool round trip in a Workspace chat stores the user message, tool call and tool result once.
  • The stored tool result is the real one from the browser, not the "Success: Function completed." placeholder.

Acceptance criteria

  • After a turn that calls a frontend tool, the conversation's messages hold the user message, one tool call and one tool result, with no repeats.
  • Reopening the chat shows the turn once.
  • A test covers a frontend-tool resume and asserts no duplicate is stored.

Notes

Activity

  1. added
    bug
    state/needs-investigation
    on Nov 16, 2026
  2. mattbrailsford commented on Nov 16, 2026

    @mattbrailsford
    ContributorAuthor
    Notes for the investigation
  3. added
    state/ready
    and removed
    state/needs-investigation
    on Nov 16, 2026
  4. mattbrailsford commented on Nov 16, 2026

    @mattbrailsford
    ContributorAuthor
    Notes for the fix

    Follows the investigation notes.

    • Repro: v18 demo site, Anthropic profile, an agent on the copilot-workspace surface. New chat: "Check what page I'm on, then …" so the agent calls get_page_info. Read umbracoAIConversationsMessage for the conversation: rows 0-2 and 3-5 are the same turn twice.

    • Cause: the server and the browser disagree about what run 1 stored, and the duplicate check matches on message ids that differ between the two copies.

      • Run 1 ends at the frontend-tool interrupt. The server stores the user message, the model's tool call and a placeholder result "Success: Function completed.".
      • The browser on purpose doesn't move its "already stored" boundary on a tool_call interrupt (run.controller.ts, #handleRunFinished, from Copilot: a restored thread can hold a tool_use without its tool_result; every later turn then fails with an Anthropic 400 #381). So the resume (run 2) resends the whole turn as plain messages: the user message, its own copy of the tool call, and the real result. There is no Resume entry, so it goes through the leading-message drop.
      • ConversationChatHistoryProvider.IsSameMessage (and IsSameContent on the write side) compares by MessageId when both sides have one. The ids differ, so neither drop matches, and all three are stored again.
    • Proof: stored rows for conversation d419d8be… (v18 demo site, with fix(copilot-workspace): Keep attachments on later turns by using the conversation id as thread id #750 applied):

      seq role messageId
      0 user 463494df… (client id)
      1 assistant (call toolu_01EBu65…) msg_011Ch6Jn… (provider id)
      2 tool (placeholder) 10129ec1… (server id)
      3 user 463494df… (same client id)
      4 assistant (same call id) c7c1435e… (client id)
      5 tool (real result) 679f1aba… (client id)

      The user message matches by id, but the drop needs the whole leading run to match the stored tail. The assistant and tool messages have different ids on each side, so the overlap check fails at every length.

    • Same cause elsewhere:

      • Approval (HITL) resumes go through the Resume branch, and the browser advances its boundary on those, so they're not affected (code read, not run).
      • v17/dev and v19/dev have the same client and server code.
      • Not checked: what the model receives in run 2. The persisted copy and the client copy both carry the same tool_use id, but Anthropic didn't reject the run.
    • Suggested fix: on the resume after a frontend tool, recognise the resent turn by the tool call id, not the message id:

      • Drop the client's copy of the user message and the tool call, keeping the server's copy, which has the provider's id and any thinking content.
      • Replace the stored placeholder result with the real result, rather than adding a second turn.
      • Test: run 1 stores the user message, call and placeholder. Run 2 resends the turn with client ids and the real result. Assert one user message, one call, and one result holding the real value.
    • Size: the full pipeline. It changes both the read-side and write-side drops, needs a way to replace a stored row, and has a design choice (fix on the server, or stop storing the placeholder in run 1).

  5. added
    state/in-progress
    and removed
    state/ready
    on Nov 16, 2026
  6. mattbrailsford commented on Nov 16, 2026

    @mattbrailsford
    ContributorAuthor
    Notes on the work
    • Route: the full feature pipeline. The fix touches both duplicate checks in the conversation store, needs a way to replace or remove stored rows, and has design choices.
    • Where: plan folder docs/plans/workspace-frontend-tool-duplicate-turn/. The branch v18/topic/workspace-frontend-tool-duplicate-turn gets cut when the build starts.
    • New findings / decisions since the notes for the fix:
      • Fix on the server by matching the resent turn by tool call id. Keep the server's copy of the call and replace the placeholder with the real result. The browser is not changed.
      • Also in scope: turns with several frontend tool calls, and a frontend tool followed by more server tools.
      • Chats already stored with a repeated turn are fixed too. The repeat is hidden when the chat loads, for the model and on screen, and its rows are removed the next time the chat is used. No migration.
      • Ports to v17/dev and v19/dev follow.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcomponent/copilot-workspacestate/in-progress

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions