Skip to content

fix(agents-api): prevent double content in streamed response finalization#3474

Closed
amikofalvy wants to merge 1 commit into
mainfrom
cursor/fix-double-content-finalize-70db
Closed

fix(agents-api): prevent double content in streamed response finalization#3474
amikofalvy wants to merge 1 commit into
mainfrom
cursor/fix-double-content-finalize-70db

Conversation

@amikofalvy

Copy link
Copy Markdown
Collaborator

Summary

Fixes a bug in IncrementalStreamParser.finalize() that caused text content to be pushed to collectedParts twice when the buffer contained incomplete artifact markers at stream end. This resulted in persisted messages having duplicate text parts, which caused "double content" when conversations were replayed in the widget.

Problem

In finalize():

  1. streamPart() was called with the remaining buffer text, unconditionally pushing it to collectedParts
  2. If hasIncompleteArtifact returned true, streamPart would NOT flush pendingTextBuffer (buffering it for later)
  3. The subsequent pendingTextBuffer cleanup block would then push the (cleaned) text to collectedParts again

This meant the same text appeared twice in the response parts array that gets persisted to the database, and then appeared doubled when the conversation was replayed via the widget.

Fix

Instead of routing the remaining buffer through streamPart() (which has the unconditional push + conditional flush split), merge it directly into pendingTextBuffer. The combined text is then cleaned of artifact tags and pushed to collectedParts exactly once.

Testing

  • Added 3 new tests covering the finalize behavior:
    • Verifies no duplicate parts when buffer has content alongside pendingTextBuffer
    • Verifies artifact tags are properly stripped from combined buffer+pendingTextBuffer
    • Verifies role is written if not yet started
  • All existing streaming tests continue to pass

Changeset

@inkeep/agents-api patch — "Fix duplicate content in streamed responses caused by double-push in finalize"

Resolves PRD-7514

Linear Issue: PRD-7514

Open in Web Open in Cursor 

… into pendingTextBuffer

The IncrementalStreamParser.finalize() method had a bug where text content
could be pushed to collectedParts twice when the buffer contained incomplete
artifact markers. streamPart() was called with the buffer text,
unconditionally pushing to collectedParts, but if hasIncompleteArtifact
returned true it wouldn't flush pendingTextBuffer. The subsequent
pendingTextBuffer cleanup would then push to collectedParts again, causing
persisted messages to contain duplicate text parts.

Fix: merge buffer directly into pendingTextBuffer in finalize() instead of
routing through streamPart(). The combined text is cleaned of artifact tags
and pushed to collectedParts exactly once.

Resolves PRD-7514
@changeset-bot

changeset-bot Bot commented Jul 21, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 1ac563d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 10 packages
Name Type
@inkeep/agents-api Patch
@inkeep/agents-manage-ui Patch
@inkeep/agents-cli Patch
@inkeep/agents-core Patch
@inkeep/agents-email Patch
@inkeep/agents-mcp Patch
@inkeep/agents-sdk Patch
@inkeep/agents-work-apps Patch
@inkeep/ai-sdk-provider Patch
@inkeep/create-agents Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions

Copy link
Copy Markdown
Contributor

Thanks for the contribution!

What happens next:

  • A maintainer will review your PR.
  • If you don't hear back within a few business days, please comment here to nudge our team.
  • This repository is maintained through an internal mirror. When your change is accepted, this PR will close automatically. Don't be alarmed when it closes — that's how it merges, and your authorship is preserved.

@amikofalvy amikofalvy closed this Jul 21, 2026
@amikofalvy
amikofalvy deleted the cursor/fix-double-content-finalize-70db branch July 21, 2026 23:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants