Title: fix(chat): stop losing sends aborted during mount-settling by j15z · Pull Request #6525 · simstudioai/sim · GitHub
Open Graph Title: fix(chat): stop losing sends aborted during mount-settling by j15z · Pull Request #6525 · simstudioai/sim
X Title: fix(chat): stop losing sends aborted during mount-settling by j15z · Pull Request #6525 · simstudioai/sim
Description: Summary Sends started on a fresh chat surface were silently dropped when the hook's unmount cleanup ran mid-flight and aborted the in-flight POST. The AbortError read as a user stop, the optimistic pair rolled back, and — because MothershipHandoffStorage.consume() clears atomically — the replacement mount found nothing left to retry. This is what made cross-route auto-send handoffs (cmd+k Ask Sim, logs "Troubleshoot in Chat") lose messages. Root cause (corrected). An earlier revision of this PR attributed the remount to "a Suspense hide/reveal cycling every effect." That is not what happens: React 19 disappears layout effects only when a mounted Suspense boundary re-suspends, and the cleanup in question (use-chat.ts, the abort('unmount:client_cleanup') teardown) is a passive effect, so a Suspense hide/reveal never runs it. Verified directly against react-dom@19.2.4: mount → layout:setup, passive:setup re-suspend → layout:cleanup ← passive:cleanup never fires reveal → layout:setup What does run it mid-flight is a genuine remount: StrictMode's dev double-mount (deterministic — it fires on every cross-route handoff in dev) and a real client-side navigation away while the send is still in flight. The cleanup's deps are all stable useCallbacks, so nothing else triggers it. Changes Idle-path sends go through the durable message queue instead of calling startSendMessage directly, so every send has a backing entry the dispatch loop can recover Aborts are detected by signal state, not error identity. Every abort() here passes a string reason, and fetch rejects with the raw reason — so err.name === 'AbortError' was false for all of them, and aborts (including user "stop") were falling through to the generic handler and surfacing Failed to send message. Fixing this is an independent correctness win A cleanup abort that strikes before the response headers arrive rolls back the optimistic pair and hands the message to recovery: the live replacement surface via the mothership-send-message event, or a one-shot stored handoff, or the restored queue entry for a chat-bound send. Attachments ride all three lanes Recovery probes the orphaned stream before re-sending. sendReachedServer only rules out a send whose response landed; the request itself may well have been accepted. app/api/mothership/chat/route.ts → lib/copilot/chat/post.ts never reads request.signal, so an accepted request still runs resolveOrCreateChat, persistUserMessage, and the billed turn to completion even though the client socket is gone. Re-sending blind would leave the user with two chats and two billed runs for one message. Recovery therefore carries the withdrawn send's userMessageId as a stream id and polls it (2.5s ceiling): resolving to a chat means the server already has the message, so that chat is adopted instead of sending again. Only a stream the server has no record of (404 — genuinely never accepted) re-sends; a timeout also re-sends, which is the safe direction Type of Change Bug fix Testing use-chat.mount-send mounts the real hook and runs the real cleanup mid-flight. Each of these fails on the unfixed hook: a cross-route handoff stays recoverable across a StrictMode double-mount (the production-shaped repro, driven by the thing that actually causes it rather than a hand-rolled unmount) an aborted chatless send is delivered to a live replacement surface, with attachments an aborted chatless send is re-persisted as a handoff when nothing claims the event a send the server already received is not re-queued recovery adopts the chat the server already created instead of sending twice (postCalls stays 1) recovery does re-send when the probe 404s Full home/hooks + stores/mothership-queue + lib/core/utils suites (364 tests), type-check, biome, and check:api-validation all pass. Known residual The probe narrows the duplicate window but does not close it: a server slower than the 2.5s ceiling still gets a second send. Closing it completely needs server-side idempotency on userMessageId in resolveOrCreateChat, which is a separate change with its own blast radius. 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 Sends started on a fresh chat surface were silently dropped when the hook's unmount cleanup ran mid-flight and aborted the in-flight POST. The AbortError read as a user stop, the optimi...
X Description: Summary Sends started on a fresh chat surface were silently dropped when the hook's unmount cleanup ran mid-flight and aborted the in-flight POST. The AbortError read as a user stop, the op...
Opengraph URL: https://github.com/simstudioai/sim/pull/6525
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:70ebae65-1781-2007-d7f1-2519c46af71e |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | BF20:143691:B5EC9:100A8D:6AFB45E7 |
| html-safe-nonce | 609c1c2f1f84815e50bad1a9241be9c99973d220ba365a12b77170756ec7bebe |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRjIwOjE0MzY5MTpCNUVDOToxMDBBOEQ6NkFGQjQ1RTciLCJ2aXNpdG9yX2lkIjoiODcwNjM3OTQ1MDc3MTI2MDkwMyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 4e938affd0c7a844d7233389c26230c60b7b74b6306440af5fb4977c31206b93 |
| hovercard-subject-tag | pull_request:4249898439 |
| 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/6525/files |
| twitter:image | https://avatars.githubusercontent.com/u/96924014?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/96924014?s=400&v=4 |
| og:image:alt | Summary Sends started on a fresh chat surface were silently dropped when the hook's unmount cleanup ran mid-flight and aborted the in-flight POST. The AbortError read as a user stop, the optimi... |
| 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