Skip to content

Commit 036bbb3

Browse files
committed
fix(mothership): honor user removal during dispatch in failure-restore path
1 parent 05edbac commit 036bbb3

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

  • apps/sim/app/workspace/[workspaceId]/home/hooks

apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,6 +1540,7 @@ export function useChat(
15401540
queueDispatchEpochRef.current++
15411541
queueDispatchActionsRef.current = []
15421542
queuedMessageDispatchIdsRef.current.clear()
1543+
userRemovedDuringDispatchRef.current.clear()
15431544
queueDispatchTaskRef.current = null
15441545
setDispatchingHeadId(null)
15451546
}, [])
@@ -1723,6 +1724,10 @@ export function useChat(
17231724
const editingQueuedId = useMothershipQueueStore((state) => state.editing[chatKey] ?? null)
17241725
const [dispatchingHeadId, setDispatchingHeadId] = useState<string | null>(null)
17251726
const queuedMessageDispatchIdsRef = useRef<Set<string>>(new Set())
1727+
// Ids the user explicitly removed while a dispatch was in flight — used to
1728+
// suppress the dispatch's failure-restore path, which would otherwise undo
1729+
// the user's removal silently.
1730+
const userRemovedDuringDispatchRef = useRef<Set<string>>(new Set())
17261731
const queueDispatchActionsRef = useRef<QueueDispatchAction[]>([])
17271732
const queueDispatchTaskRef = useRef<Promise<void> | null>(null)
17281733
const queueDispatchEpochRef = useRef(0)
@@ -5529,6 +5534,11 @@ export function useChat(
55295534
if (!removedFromQueue || options.epoch !== queueDispatchEpochRef.current) {
55305535
return
55315536
}
5537+
// If the user explicitly removed this message during dispatch, honor
5538+
// that and don't re-insert on failure.
5539+
if (userRemovedDuringDispatchRef.current.delete(msg.id)) {
5540+
return
5541+
}
55325542
useMothershipQueueStore.getState().insertAt(dispatchChatKey, originalIndex, msg)
55335543
}
55345544

@@ -5564,6 +5574,7 @@ export function useChat(
55645574
} finally {
55655575
setDispatchingHeadId((current) => (current === msg.id ? null : current))
55665576
queuedMessageDispatchIdsRef.current.delete(msg.id)
5577+
userRemovedDuringDispatchRef.current.delete(msg.id)
55675578
}
55685579
},
55695580
[startSendMessage]
@@ -5616,6 +5627,11 @@ export function useChat(
56165627
enqueueQueueDispatchRef.current = enqueueQueueDispatch
56175628

56185629
const removeFromQueue = useCallback((id: string) => {
5630+
// If the message is mid-dispatch, mark it so the dispatch's failure-restore
5631+
// path won't silently undo the user's removal.
5632+
if (queuedMessageDispatchIdsRef.current.has(id)) {
5633+
userRemovedDuringDispatchRef.current.add(id)
5634+
}
56195635
clearQueuedSendHandoffState(id)
56205636
clearQueuedSendHandoffClaim(id)
56215637
useMothershipQueueStore.getState().remove(chatKeyRef.current, id)

0 commit comments

Comments
 (0)