Skip to content

fix(chat): deduplicate chat sends server-side instead of probing for them - #6536

Merged
waleedlatif1 merged 4 commits into
stagingfrom
fix/chat-send-idempotency
Aug 11, 2026
Merged

fix(chat): deduplicate chat sends server-side instead of probing for them#6536
waleedlatif1 merged 4 commits into
stagingfrom
fix/chat-send-idempotency

Conversation

@waleedlatif1

@waleedlatif1waleedlatif1 commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

Summary

Follow-up to #6525, which fixed a real bug — a send withdrawn by an unmount cleanup was silently lost — but paid for it with a client-side probe. This replaces that probe with the deduplication mechanism the codebase already owns, and deletes more than it adds.

The problem #6525 had to solve. A client cannot tell whether a request it aborted reached the server. lib/copilot/chat/post.ts never reads request.signal, so a request the server accepted still runs resolveOrCreateChat, persistUserMessage, and the billed turn to completion after the browser drops the socket (Next.js discussion). Retrying blind meant two chats and two billed runs; not retrying meant losing the message.

How #6525 answered it. It polled the orphaned stream for up to 2.5s before deciding. That number was a guess with no production timing behind it, the poll had to distinguish "the server has no such stream" from "we stopped looking" (conflating them re-sent after teardown, opening an uncancellable POST), and it still left a window open for a server slower than the ceiling.

How this answers it.IdempotencyService already backs webhook, polling, and billing deduplication here, and billingIdempotency exists for exactly this hazard — its own comment says a retry "would double-record usage — real money". Chat sends now claim through it, keyed on the client-generated userMessageId. Retrying is just reusing the id.

Server

  • chatSendIdempotency claims each send. The key is scoped to the authenticated useruserMessageId is client-supplied, so an unscoped key would let one user probe another's sends and read back their chat id
  • Storage is forced to Postgres for the same reason as billingIdempotency: a missed dedup is a second billed turn, so the key must survive Redis memory pressure. ~1-5ms is invisible next to the LLM call that follows
  • The chat is recorded against the claim as soon as it resolves — the earliest a retry can be answered with somewhere to go. From there the claim is permanent; before there, it is released so a failed send stays retryable
  • A repeat gets 409 naming the chat, deliberately the shape the pending-stream lock already returns, so the client's existing conflict handler reattaches rather than starting a turn

It fails open at every step. Deduplication saves a duplicate chat; the send is the user's message. An unreachable bookkeeping store must degrade chat, never take it down — an early draft failed closed and turned every send into a 409, which is exactly the outcome to avoid in production.

Client

Deleted with the probe: the poll and its two constants, the three-state result, the epoch plumbing that kept a superseded poll from re-sending, and the chat-adoption branch it required. The hook nets 67 lines smaller.

  • Retry carries resumeUserMessageId through the same lanes fix(chat): stop losing sends aborted during mount-settling #6525 built (live event, stored handoff, queue entry) — same plumbing, a payload that is consumed synchronously instead of awaited
  • Idle sends go back to calling startSendMessage directly.fix(chat): stop losing sends aborted during mount-settling #6525 routed them through the durable queue so recovery had a backing entry, which put every message in the product through the queue store, sessionStorage, and the dispatch loop for a rare path. Recovery never needed it — message, attachments, contexts, and id are all in scope at the abort. Both callers now share one handOffWithdrawnSend
  • Removing the probe also removes a race it introduced: it awaited up to 2.5s between dispatchQueuedMessage's "re-read live" and the send, so an edit during that window sent pre-edit text
  • startSendMessage takes its optional tail as an options object; it was at six positional parameters and the retry id would have been a seventh

Testing

Server: deduplicates a repeat without opening a second chat or stream, scopes the key per user, records the chat against the send, and still sends when the claim store is down. Client: reuses the original id on retry, and adopts the chat a deduplicated retry names. Plus the coverage #6525 earned — StrictMode double-mount, live-surface delivery, stored-handoff fallback, and the departing surface not claiming its own recovery event.

Each new test was confirmed red without its fix. 2058 tests, type-check, biome, and check:api-validation pass.

Known residuals

All three are deliberate, and each is better than what main or staging does in the same scenario.

Losing the claim store loses deduplication for that window — the fail-open trade above. The dedup window is 1 hour; userMessageId is never reused, so a longer one would be harmless.

A concurrent retry can be told to reattach to a turn that never starts. A retry arriving between storeResult and the turn starting gets 409 and reattaches; if the original then also fails, there is nothing to attach to. The client keeps the optimistic pair (the generic 409 branch does not roll back) and finalizes with an error, so the user sees their message with a failed response rather than losing it — and the original's finally releases the claim, so sending again starts a fresh turn. main loses the message with no error; staging re-sends and can duplicate the turn.

A failed storeResult degrades to no deduplication after 60s. The claim stays in-progress, which atomicallyClaimDb lets a retry reclaim once the lease expires, so a retry more than 60s later can start a second billed turn. Every alternative is worse: releasing on record failure duplicates immediately, and extending the lease to cover a turn (maxDuration is 3600s) lets a crashed pod poison the id for an hour. Leaving it in-progress still deduplicates for the first 60s, which is where recovery retries actually land — they fire from the unmount cleanup within milliseconds. Requires a Postgres write failure to reach, and logs Could not record the chat for this send when it happens.

Claim lifecycle

The load-bearing invariant, since it is easy to get wrong: the claim is permanent only once a turn is streaming. One line clears it, immediately before the stream response; finally releases it on every other exit.

ExitTurn startedClaim
Duplicate (409)not ours, untouched
Claim store unavailablen/anever taken
Rejected branch (400)noreleased
Chat not found (404)noreleased
Stream collision (409)noreleased
Throw (500)noreleased
Stream returned (200)yeskept, 1h

Type of Change

  • Bug fix
  • Refactor

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

…them
A client cannot tell whether a request it aborted reached the server: the chat
route never reads `request.signal`, so an accepted one still opens the chat,
persists the user message, and bills the turn after the socket drops. #6525
answered that by polling the orphaned stream before retrying — a 2.5s guess
that had to distinguish "no such stream" from "we stopped looking", and still
left a window open.
The codebase already owns the right tool. `IdempotencyService` backs webhook,
polling, and billing dedup, and `billingIdempotency` exists for exactly this
hazard: "a retry would double-record usage — real money". Chat sends now claim
the same way, keyed on the client-generated `userMessageId` and scoped to the
caller so nobody can probe another user's sends. A repeat gets 409 naming the
chat the first attempt opened — deliberately the shape the pending-stream lock
already returns, so the client's existing conflict handler reattaches instead
of starting a turn, with only the chat-adoption line added.
The claim fails open at every step. Deduplication saves a duplicate chat; the
send IS the user's message, so an unreachable bookkeeping store degrades chat
rather than taking it down. It is released when a send fails before recording a
chat, and deliberately kept once recorded.
Retrying now just reuses the id, which deletes the probe outright: the poll and
its two constants, the three-state result, the epoch plumbing that kept a
superseded poll from re-sending, and the chat-adoption branch it needed. The
client hook nets 67 lines smaller.
Idle sends go back to calling `startSendMessage` directly. #6525 routed them
through the durable queue so recovery had a backing entry, which put every
message in the product through the queue store, sessionStorage, and the
dispatch loop for the sake of a rare path — and the recovery never needed it,
since the message, attachments, contexts, and id are all in scope at the abort.
Both callers now share one `handOffWithdrawnSend`.
`startSendMessage` takes its optional tail as an options object; it was at six
positional parameters and the retry id would have been a seventh.
Tests cover both halves: the server dedups, scopes the key per user, records
the chat, and still sends when the claim store is down; the client reuses the
original id on retry and adopts the chat a deduplicated retry names. Each was
confirmed red without its fix.
@vercel

vercelBot commented Aug 11, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
ProjectDeploymentActionsUpdated (UTC)
docsSkippedSkippedAug 11, 2026 8:10am

Request Review

@cursor

cursorBot commented Aug 11, 2026

Copy link
Copy Markdown

PR Summary

High Risk
Changes chat POST idempotency, billing-related duplicate-turn behavior, and core useChat send/recovery paths; mistakes could double-bill, lose messages, or strand sends across chat switches.

Overview
Replaces the client’s orphaned-stream probe (recoverStreamId) with server-side send deduplication keyed on the client’s userMessageId, so retries after an unmount-aborted POST don’t have to guess whether the server accepted the first request.

Server: Adds chatSendIdempotency (Postgres-backed, user-scoped claims) on the unified chat POST handler. Replays return 409 with activeStreamId and optional chatId (same shape as stream collisions). Claims are released when no turn actually streams; they become permanent once the SSE response is returned. Claim/store failures fail open so chat still works.

Client: Recovery plumbing now carries resumeUserMessageId through handoffs, events, and the queue. Withdrawn sends are handed off via shared handOffWithdrawnSend; chat-bound withdrawals re-queue under the stable chat key instead of following the user to another surface. Idle sends go direct to startSendMessage again (not forced through the durable queue). On 409 dedup, the hook can adopt chatId from the error and reconnect. Large removal of probe polling, adoption branches, and related queue dispatch logic in use-chat.

Tests updated/added for remount recovery, dedup reuse of ids, 409 adoption, and server claim lifecycle.

Reviewed by Cursor Bugbot for commit 9ab60fa. Configure here.

@greptile-apps

greptile-appsBot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This follow-up replaces client-side probing of withdrawn chat sends with server-side idempotency keyed by the original user-message ID.

  • Adds user-scoped, Postgres-backed chat-send claims and returns existing chat/stream information for duplicates.
  • Carries the original message ID through live events, stored handoffs, and queued retries.
  • Refactors direct and queued send recovery while adding coverage for deduplication, remount recovery, and chat-bound retries.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

FilenameOverview
apps/sim/lib/copilot/chat/post.tsAdds user-scoped chat-send claiming, duplicate responses, chat-result recording, and lifecycle-aware release behavior; the previously reported lifecycle defects are addressed or explicitly accepted.
apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.tsReplaces orphan-stream probing with message-ID reuse and routes withdrawn sends through chat-appropriate handoff or queue recovery.
apps/sim/lib/core/idempotency/service.tsIntroduces a Postgres-backed chat-send idempotency service with one-hour completed retention and a short in-progress lease.
apps/sim/lib/copilot/chat/post.test.tsAdds server coverage for duplicate claims, user scoping, result recording, lifecycle release, and fail-open behavior.
apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.mount-send.test.tsxUpdates remount recovery tests to verify message-ID reuse, duplicate-chat adoption, and stable chat-bound queueing.

Sequence Diagram

sequenceDiagram
participant UI as Chat UI
participant API as Chat POST
participant Idem as Postgres Idempotency
participant Turn as Chat Turn
UI->>API: Send(userMessageId)
API->>Idem: Atomically claim user-scoped ID
alt First attempt
Idem-->>API: Claimed
API->>API: Resolve chat and record chatId
API->>Turn: Persist message and start stream
API-->>UI: SSE response
else Duplicate attempt
Idem-->>API: Existing claim/result
API-->>UI: 409 with activeStreamId and chatId
UI->>Turn: Reconnect to original stream
end
Loading

Reviews (5): Last reviewed commit: "refactor(chat): give the send claim a si..." | Re-trigger Greptile

Comment threadapps/sim/lib/copilot/chat/post.ts Outdated
Comment threadapps/sim/lib/copilot/chat/post.ts
Comment threadapps/sim/lib/copilot/chat/post.ts Outdated
Comment threadapps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts
…d claims
Audit follow-ups, two of them real defects in the previous commit.
A withdrawn send routed unconditionally through the cross-surface lanes. Those
deliver to whatever chat is mounted next, so sending in one chat and switching
to another re-sent the message into the second one. The dispatcher already drew
the distinction; the idle path now draws it too — a chat-bound key is the stable
chat id, so re-queueing under it both retries durably and keeps the message
where the user put it. Only a chatless key, which dies with its mount, goes to
the lanes.
The claim release sat in `catch`, so the two paths that return a response
without throwing — a rejected branch, and a missing chat — stranded an
in-progress claim for its full 60s TTL, and a retry inside that window got a
spurious "already sent" instead of the real error. Moved to `finally`.
Also: `userMessageId` is now length-bounded, since it becomes part of a Postgres
key and an oversized one would throw inside the claim; `requestId` was still
empty at claim time, so both dedup logs printed a blank prefix; the provider
segment said `mothership` on a handler that also serves the workflow copilot,
and now says what the key identifies; `retryFailures` was dead config, only read
by `executeWithIdempotency`, which this caller never invokes; the doc pointed at
`billingIdempotency`, which has no consumers, and now points at the live Stripe
analogue.
Trimmed: `sendClaimRecorded` folded into clearing `sendClaim`, the unread `kind`
discriminant dropped from a one-arm union, the single-use `claimedChatId`
inlined, and the prose on all three of those cut back to what the code does not
already say.
@waleedlatif1

Copy link
Copy Markdown
CollaboratorAuthor

@greptile

@waleedlatif1

Copy link
Copy Markdown
CollaboratorAuthor

@cursor review

Comment threadapps/sim/lib/copilot/chat/post.ts
The claim became permanent as soon as the chat resolved, but three exits still
return without starting a turn — a rejected branch, a missing chat, and a
pending-stream collision. The last one matters: the queued-send-handoff path
deliberately retries under the original `userMessageId` after a collision, and
against a permanent claim that retry deduplicated to a chat whose turn never
ran, reattaching to a stream that does not exist. A send that had merely
collided became unsendable for the claim's full hour.
The claim is now dropped immediately before the stream response is returned, so
`finally` releases it on every other exit. Recording the chat still happens as
early as possible — a concurrent duplicate needs somewhere to go — it just no
longer implies the turn happened.
@waleedlatif1

Copy link
Copy Markdown
CollaboratorAuthor

@greptile

@waleedlatif1

Copy link
Copy Markdown
CollaboratorAuthor

@cursor review

Recording the chat also dropped the claim when it failed, which left a second
way for a claim to stop being tracked and a compound hole behind it: a failed
record followed by a throw stranded the claim for its in-progress TTL, and a
retry inside that window reattached to a turn that never started.
Only one line now decides permanence — the claim is cleared immediately before
the stream response — so `finally` releases it on every exit that did not start
a turn, including a failed record. The `recorded` flag is gone with it.
Covers the 400 early return with a release assertion: that path returns without
throwing, so it is the one that proves the release has to live in `finally`.
@waleedlatif1

Copy link
Copy Markdown
CollaboratorAuthor

@greptile

@waleedlatif1

Copy link
Copy Markdown
CollaboratorAuthor

@cursor review

Comment threadapps/sim/lib/copilot/chat/post.ts
Comment threadapps/sim/lib/copilot/chat/post.ts
@waleedlatif1

Copy link
Copy Markdown
CollaboratorAuthor

@greptile

@waleedlatif1

Copy link
Copy Markdown
CollaboratorAuthor

@cursor review

@cursorcursorBot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 9ab60fa. Configure here.

@cursorcursorBot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 9ab60fa. Configure here.

@waleedlatif1
waleedlatif1 merged commit f8644cc into stagingAug 11, 2026
31 checks passed
@waleedlatif1
waleedlatif1 deleted the fix/chat-send-idempotency branch August 11, 2026 14:58
Sign up for freeto 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.

1 participant

@waleedlatif1