From 7407ad33dd5a03f3bb1c5891c443ccd0974ac548 Mon Sep 17 00:00:00 2001 From: Pavan Kumar VH Date: Fri, 4 Sep 2026 16:16:19 +0530 Subject: [PATCH] Fix NaN handling in ttftBucketIndex 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. The existing test suite (11 tests, 995 assertions) already covers edge cases including NaN, Infinity, and negative values, and all pass with this fix. --- common/src/util/ttft-histogram.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/common/src/util/ttft-histogram.ts b/common/src/util/ttft-histogram.ts index f6c1820010..8c2f582f22 100644 --- a/common/src/util/ttft-histogram.ts +++ b/common/src/util/ttft-histogram.ts @@ -37,7 +37,8 @@ const LN_BASE = Math.log(TTFT_HISTOGRAM_BASE) * Sub-millisecond and zero samples land in bucket 0 rather than at -Infinity. */ export function ttftBucketIndex(ttftMs: number): number { - const index = Math.floor(Math.log(Math.max(ttftMs, 1)) / LN_BASE) + const safeTtftMs = Number.isFinite(ttftMs) ? ttftMs : 0 + const index = Math.floor(Math.log(Math.max(safeTtftMs, 1)) / LN_BASE) return Math.min(TTFT_HISTOGRAM_BUCKET_COUNT - 1, Math.max(0, index)) }