fix: prevent premature queued message auto-send

Only auto-send queued messages after an active session becomes idle
Avoid treating missing initial status as safe to send
Add coverage for queue auto-send status transitions
This commit is contained in:
Bohdan Triapitsyn
2026-05-27 23:36:43 +03:00
parent 5331c4d962
commit 461e2f482c
2 changed files with 26 additions and 6 deletions
@@ -106,6 +106,14 @@ const resolveSessionSendConfig = (sessionId: string) => {
};
};
export const shouldDispatchQueuedAutoSend = (
previousStatusType: SessionStatusType | undefined,
currentStatusType: SessionStatusType,
): boolean => {
return (previousStatusType === 'busy' || previousStatusType === 'retry')
&& currentStatusType === 'idle';
};
export function useQueuedMessageAutoSend(enabledOrOptions?: boolean | { enabled?: boolean }) {
const enabled = typeof enabledOrOptions === 'boolean' ? enabledOrOptions : (enabledOrOptions?.enabled ?? true);
const queuedMessages = useMessageQueueStore((state) => state.queuedMessages);
@@ -183,12 +191,8 @@ export function useQueuedMessageAutoSend(enabledOrOptions?: boolean | { enabled?
queueEntries.forEach(([sessionId, queue]) => {
const currentStatusType = (statusRecord[sessionId]?.type ?? 'idle') as SessionStatusType;
const previousStatusType = previousStatusRef.current.get(sessionId);
const becameIdle =
(previousStatusType === 'busy' || previousStatusType === 'retry')
&& currentStatusType === 'idle';
const firstSeenIdle = previousStatusType === undefined && currentStatusType === 'idle';
if (queue.length > 0 && (becameIdle || firstSeenIdle)) {
if (queue.length > 0 && shouldDispatchQueuedAutoSend(previousStatusType, currentStatusType)) {
void dispatchSessionQueue(sessionId, queue);
}