Title: fix(chat): deduplicate chat sends server-side instead of probing for them by waleedlatif1 · Pull Request #6536 · simstudioai/sim · GitHub
Open Graph Title: fix(chat): deduplicate chat sends server-side instead of probing for them by waleedlatif1 · Pull Request #6536 · simstudioai/sim
X Title: fix(chat): deduplicate chat sends server-side instead of probing for them by waleedlatif1 · Pull Request #6536 · simstudioai/sim
Description: 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 user — userMessageId 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 #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. #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. Exit Turn started Claim Duplicate (409) — not ours, untouched Claim store unavailable n/a never taken Rejected branch (400) no released Chat not found (404) no released Stream collision (409) no released Throw (500) no released Stream returned (200) yes kept, 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)
Open Graph Description: 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 deduplica...
X Description: 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 deduplica...
Opengraph URL: https://github.com/simstudioai/sim/pull/6536
X: @github
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:d7a18c87-3122-6053-b047-ddff35d54b25 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 83E4:15DF38:733F8E:9E1D1A:6AFB42C7 |
| html-safe-nonce | acb837bec89b5256c0a6f26373dd81d6588d444f1e9c3e189bb9d4f5486984d1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4M0U0OjE1REYzODo3MzNGOEU6OUUxRDFBOjZBRkI0MkM3IiwidmlzaXRvcl9pZCI6Ijg0OTIzMzkwNjgzODAzMzI3NDMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 3117ad1643be8070caaab8fb2bc37ac232fb8165fd260b4f8708910969bc6f3e |
| hovercard-subject-tag | pull_request:4251265279 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/simstudioai/sim/pull/6536/files |
| twitter:image | https://avatars.githubusercontent.com/u/40672544?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/40672544?s=400&v=4 |
| og:image:alt | 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 deduplica... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | d849b928f455c800985398e7f1b659425adb312a5fa14fba5d9a14e62e9fe5dd |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/simstudioai/sim git https://github.com/simstudioai/sim.git |
| octolytics-dimension-user_id | 199344406 |
| octolytics-dimension-user_login | simstudioai |
| octolytics-dimension-repository_id | 912559512 |
| octolytics-dimension-repository_nwo | simstudioai/sim |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 912559512 |
| octolytics-dimension-repository_network_root_nwo | simstudioai/sim |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | a6c2f87cbcb6414452377b4967858ed6a66f1109 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width