fix(chat): O(1) token cleanup in useChatSend via reverse messageId map (mc#2908 F8) #3033
Reference in New Issue
Block a user
Delete Branch "fix/mobile-chat-f8-token-cleanup-omap"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Fixes F8 from mobile-chat audit #2908.
finishSendTokenpreviously scannedmessageIdToTokenReflinearly to delete the mapping for a completed token. This PR adds atokenToMessageIdRefreverse map and maintains both indices when a send starts, so cleanup is O(1) per send completion regardless of concurrent-send count.Validation
npm run buildincanvas/passes.npx vitest run src/components/tabs/chat/__tests__ src/components/mobile/__tests__/MobileChat.test.tsxpasses (341/341).Relates-to: #2908
APPROVED after independent 5-axis review.
Correctness: the new token-to-messageId reverse map is populated at the same point as
messageIdToTokenRefand is cleaned through the existingfinishSendTokenpath, so messageId completion, queued/timeout transitions, and error cleanup keep both maps consistent. Robustness: finish remains idempotent for raced WS/HTTP completions. Security: no auth/data exposure change. Performance: removes the linear scan on cleanup. Readability: the bidirectional index is understandable and localized.APPROVED. 5-axis review on head
55c985d2.Correctness: tokenToMessageIdRef is populated at the same point messageIdToTokenRef is populated, and finishSendToken now removes both maps by token in O(1). Existing finish-by-message-id and legacy fallback flows still converge through finishSendToken, so cleanup remains centralized.
Robustness/security/performance: no external input/auth surface changes. The reverse map avoids the prior linear scan without changing send completion semantics; stale entries are removed on normal finish paths.
Readability: the bidirectional mapping is understandable and the comment explains the reason. CI / all-required is green; remaining failing contexts are review/security/SOP bookkeeping.