fix(bb-knowledge-base): validate non-integer retrieve maxResults - #449
Open
osama-rizk wants to merge 1 commit into
Open
fix(bb-knowledge-base): validate non-integer retrieve maxResults#449osama-rizk wants to merge 1 commit into
osama-rizk wants to merge 1 commit into
Conversation
maxResults was normalized with Math.min(Math.max(v ?? 10, 1), 100), which silently passed fractional/non-finite values (1.5, NaN, Infinity) through — diverging the mock from the AWS RetrieveCommand, and Bedrock rejects a non-integer numberOfResults. Add a shared normalizeMaxResults helper used by both the mock and AWS runtimes: keep the documented clamp for finite integers, reject fractional/ non-finite values with KnowledgeBaseErrors.ValidationError before any search or Bedrock request. Also hoist the duplicated blocksError helper into errors.ts. Fixes #430.
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.
Fixes #430.
Problem
KnowledgeBase.retrievenormalizedmaxResultswithMath.min(Math.max(options?.maxResults ?? 10, 1), 100)in bothindex.mock.tsandindex.aws.ts. That clamp silently preserves fractional and non-finite values:maxResults: 1.5→ the mock slices its result array by1.5; the AWS runtime sendsnumberOfResults: 1.5.maxResults: NaN→ the mock returns an empty set; the AWS runtime sendsnumberOfResults: NaN.Bedrock defines
numberOfResultsas an integer in 1–100, so neither is a valid request — and the mock and AWS runtimes diverge on exactly these inputs (which can arise when the option comes from user input or a calculation).Fix
A shared
normalizeMaxResultshelper (newnormalize.ts), used by both runtimes:undefined→ default10.0/-5→1,200→100).1.5,NaN,±Infinity) → rejected withKnowledgeBaseErrors.ValidationError, before any local search or Bedrock request.Also hoisted the
blocksErrorhelper (duplicated verbatim in both entries) intoerrors.tsas the single source, and clarified theRetrieveOptions.maxResultsdoc.Tests (red → green)
normalize.test.ts— default, pass-through, clamp bounds, and rejection of1.5/NaN/±Infinity.index.mock.test.ts—retrieverejects1.5/NaNwithValidationError(parity with AWS).index.aws.test.ts—retrieverejects1.5/NaNwithValidationErrorand sends noRetrieveCommand.Verification
@aws-blocks/bb-knowledge-basesuite: 139 pass / 0 fail.check:api✅ (no public-API change — helper stays internal);npm run lint0 errors; umbrella-changeset guard ✅.Changeset
@aws-blocks/bb-knowledge-basepatch +@aws-blocks/blockspatch (umbrella re-export).