Skip to content

fix(bb-knowledge-base): validate non-integer retrieve maxResults - #449

Open
osama-rizk wants to merge 1 commit into
mainfrom
fix/kb-validate-maxresults
Open

fix(bb-knowledge-base): validate non-integer retrieve maxResults#449
osama-rizk wants to merge 1 commit into
mainfrom
fix/kb-validate-maxresults

Conversation

@osama-rizk

Copy link
Copy Markdown
Contributor

Fixes #430.

Problem

KnowledgeBase.retrieve normalized maxResults with Math.min(Math.max(options?.maxResults ?? 10, 1), 100) in both index.mock.ts and index.aws.ts. That clamp silently preserves fractional and non-finite values:

  • maxResults: 1.5 → the mock slices its result array by 1.5; the AWS runtime sends numberOfResults: 1.5.
  • maxResults: NaN → the mock returns an empty set; the AWS runtime sends numberOfResults: NaN.

Bedrock defines numberOfResults as 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 normalizeMaxResults helper (new normalize.ts), used by both runtimes:

  • undefined → default 10.
  • Finite integer → clamped to 1–100 (documented behavior preserved; 0/-51, 200100).
  • Fractional or non-finite (1.5, NaN, ±Infinity) → rejected with KnowledgeBaseErrors.ValidationError, before any local search or Bedrock request.

Also hoisted the blocksError helper (duplicated verbatim in both entries) into errors.ts as the single source, and clarified the RetrieveOptions.maxResults doc.

Tests (red → green)

  • normalize.test.ts — default, pass-through, clamp bounds, and rejection of 1.5/NaN/±Infinity.
  • index.mock.test.tsretrieve rejects 1.5/NaN with ValidationError (parity with AWS).
  • index.aws.test.tsretrieve rejects 1.5/NaN with ValidationError and sends no RetrieveCommand.

Verification

  • @aws-blocks/bb-knowledge-base suite: 139 pass / 0 fail.
  • Conditional-export parity 21/21; check:api ✅ (no public-API change — helper stays internal); npm run lint 0 errors; umbrella-changeset guard ✅.

Changeset

@aws-blocks/bb-knowledge-base patch + @aws-blocks/blocks patch (umbrella re-export).

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.
@osama-rizk
osama-rizk requested a review from a team as a code owner August 27, 2026 11:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(bb-knowledge-base): validate non-integer retrieve maxResults

1 participant