Skip to content

Fix NaN handling in ttftBucketIndex - #1238

Open
pavankumar-vh wants to merge 1 commit into
CodebuffAI:mainfrom
pavankumar-vh:fix/ttft-histogram-nan-handling
Open

Fix NaN handling in ttftBucketIndex#1238
pavankumar-vh wants to merge 1 commit into
CodebuffAI:mainfrom
pavankumar-vh:fix/ttft-histogram-nan-handling

Conversation

@pavankumar-vh

Copy link
Copy Markdown

Overview

Fix NaN handling in the ttftBucketIndex function in common/src/util/ttft-histogram.ts.

Bug Description

The function didn't validate that ttftMs is a finite number. If ttftMs was NaN or Infinity, Math.max(NaN, 1) would return NaN, causing Math.log(NaN) to return NaN, and the entire calculation would produce NaN.

Fix

Added Number.isFinite() check to default to 0 for invalid numbers.

Testing

No existing tests for this function, but the fix prevents incorrect behavior with invalid inputs.

Files Changed

  • common/src/util/ttft-histogram.ts - Added NaN validation

Scope

This change only touches common/ which is an approved contribution area per the Contributing Guide.

The function didn't validate that ttftMs is a finite number. If ttftMs was
NaN or Infinity, Math.max(NaN, 1) would return NaN, causing Math.log(NaN)
to return NaN, and the entire calculation would produce NaN.

Added Number.isFinite() check to default to 0 for invalid numbers.
@codebuff-team

Copy link
Copy Markdown
Contributor

Good catch — Math.max(NaN, 1) does indeed return NaN, and without a finite check, a stray NaN/Infinity value for ttftMs would propagate through Math.log and blow up the bucket index. The fix is minimal and correct: defaulting to 0 folds bad input into bucket 0, which matches the existing behavior for sub-millisecond/zero samples described in the docstring above.

One suggestion: since the module has no existing tests, it'd strengthen this PR to add a couple of unit tests in common/src/util/ttft-histogram.test.ts (or wherever colocated tests live) covering NaN, Infinity, -Infinity, and negative input, since this function has no test coverage today and it's cheap to add given how small the surface area is. Not a blocker for porting, but worth doing if you want the change accepted with confidence.

Overall this is exactly the kind of small, scoped, well-reasoned fix that's easy to review and port.

@codebuff-team codebuff-team added bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree labels Sep 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:triaged Classified by the community triage bot pr:port-candidate Worth porting into the private source tree

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants