fix(mothership): persist @-mentioned resources across send#4587
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
PR SummaryMedium Risk Overview Changes hydration to merge server resources with any local-only resources (instead of replacing), and adds logic to persist resource reorders (and defer reorder persistence until pending/in-flight adds settle). Also migrates several fire-and-forget Reviewed by Cursor Bugbot for commit f48fa3a. Configure here. |
Greptile SummaryThis PR fixes @-mentioned resources disappearing after send by introducing a local-first, merge-on-hydrate approach. Resources added before a chat ID exists are queued in
Confidence Score: 5/5Safe to merge; the core persistence, race-condition, and reorder bugs are all correctly addressed. All three key scenarios — pre-chat resource queueing, ADD/DELETE ordering via in-flight tracking, and deferred reorder PATCHing — are handled correctly. The one ordering edge case only affects sort order during a narrow timing window and self-corrects on next hydration. No files require special attention; the change is self-contained in use-chat.ts. Important Files Changed
Sequence DiagramsequenceDiagram
participant U as User
participant H as useChat (hook)
participant S as Server
Note over U,H: Before first send (no chatId)
U->>H: addResource(A)
H->>H: pendingPersistResourceKeysRef.add(type:A)
U->>H: reorderResources([A, B])
H->>H: "reorderNeededAfterFlushRef = true"
U->>H: sendMessage()
H->>S: POST /chat
S-->>H: chatId assigned
H->>H: "adoptResolvedChatId -> flushPendingResources(chatId)"
loop each pending resource
H->>S: POST addResource(chatId, A)
S-->>H: 200 OK
end
H->>H: "reorderNeededAfterFlushRef=true -> fire PATCH"
H->>S: PATCH reorderResources(chatId, [A,B])
Note over U,H: Remove resource with in-flight ADD
U->>H: addResource(C) in-flight
U->>H: removeResource(C)
H->>H: inFlightAdd.finally(fireDelete)
S-->>H: ADD resolves
H->>S: DELETE removeResource(chatId, C)
Note over U,H: Hydration merge
S-->>H: "chatHistory.resources=[A,B]"
H->>H: "localOnly = resourcesRef minus serverKeys"
H->>H: setResources([...server,...localOnly])
Reviews (3): Last reviewed commit: "fix(mship-resources): defer reorder when..." | Re-trigger Greptile |
…ng flush - Track in-flight ADD promises so DELETE chains off finally(), preventing orphaned server rows when a user removes a resource before its POST resolves - Defer reorder PATCH until pending flush completes; emit with full local order - Clear new refs in reset paths
|
@greptile |
|
@cursor review |
…ng chat reorderResources previously only checked pendingPersistResourceKeysRef. When a chatId exists, addResource fires the POST immediately and only tracks the promise in inFlightResourceAddsRef — so a reorder before those ADDs settle shipped a PATCH the server rejected, and the silent catch lost the reorder. Now treat in-flight ADDs like pending ones: defer the PATCH and replay it after Promise.allSettled on the in-flight map.
|
@greptile |
|
@cursor review |
There was a problem hiding this comment.
✅ 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 f48fa3a. Configure here.
Summary
server ∪ localinstead of replacing — optimistic items survive any refetchrequestJson(contract, ...)for type-safe request shapes; remaining raw fetches kept for SSE streaming + traceparent header propagationType of Change
Testing
Tested manually.
bun run check:api-validationpasses;tsc --noEmitclean.Checklist