fix(cdk): follow LastEvaluatedKey when looking up a chat or system context by id - #1678
Open
takke1986 wants to merge 1 commit into
Open
fix(cdk): follow LastEvaluatedKey when looking up a chat or system context by id#1678takke1986 wants to merge 1 commit into
takke1986 wants to merge 1 commit into
Conversation
…ntext by id findChatById issued a single Query with a FilterExpression and ignored LastEvaluatedKey. DynamoDB reads up to 1 MB per page and applies FilterExpression after that read, so for a user whose partition exceeds 1 MB the target chat can sit on a later page and the lookup returns null even though the chat exists. Extract a queryFirstMatch helper that follows LastEvaluatedKey until a match is found or the pages are exhausted, and use it for findChatById. findSystemContextById had the identical pattern and is fixed the same way. A survey of repository.ts shows these were the only two functions combining FilterExpression with no pagination; listChats already paginates and the remaining queries do not filter.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description of Changes
findChatByIdissued a singleQuerywith aFilterExpressionand ignoredLastEvaluatedKey:DynamoDB reads up to 1 MB per page and applies
FilterExpressionafter that read. For a user whoseuser#<id>partition exceeds 1 MB, the target chat can sit on a later page, so the function returnsnulleven though the chat exists. This is what #1663 reports: heavy users stop being able to save new messages.This PR extracts a small
queryFirstMatchhelper that followsLastEvaluatedKeyuntil a match is found or the pages are exhausted, and uses it forfindChatById.findSystemContextByIdhad the identical pattern (singleQuery+FilterExpression, no pagination), so it is fixed the same way. It is not covered by an existing issue; I found it while checking for other occurrences. A survey ofrepository.tsshows these were the only two —listChatsalready paginates, and the remaining queries do not useFilterExpression.Compatibility: no signature or return-type change. The only difference is that a lookup which previously returned
nullfor a large partition now returns the item. For partitions under 1 MB the behavior and the number of API calls are unchanged.Checklist
npm run cdk:test— 45 tests / 15 snapshots passed, no snapshot differencesNew unit tests in
packages/cdk/test/lambda/repository.test.tscover:LastEvaluatedKeybeing passed asExclusiveStartKeyon the following requestnullonce the pages are exhaustedThese tests fail against the previous implementation (4 of the 6 fail; the 2 that pass are the controls — a first-page match and a single-page miss), so they characterize the bug rather than the fix.
Also run:
npm run web:test— 278 passedtsc --noEmit— no errorseslintandprettier --checkclean on all changed filesRelated Issues