Skip to content

Fix negative streak handling in getFreebuffStreakGlmWeeklyUnits - #1261

Open
pavankumar-vh wants to merge 1 commit into
CodebuffAI:mainfrom
pavankumar-vh:fix/streak-negative-test
Open

Fix negative streak handling in getFreebuffStreakGlmWeeklyUnits#1261
pavankumar-vh wants to merge 1 commit into
CodebuffAI:mainfrom
pavankumar-vh:fix/streak-negative-test

Conversation

@pavankumar-vh

Copy link
Copy Markdown

Overview

Fix negative streak handling in the getFreebuffStreakGlmWeeklyUnits function in common/src/util/freebuff-streak.ts.

Bug Description

The function didn't validate that streak is non-negative. If streak was negative, Math.floor(streak / INTERVAL) would be negative, and Math.min with the positive max would return the negative value, resulting in a negative GLM bonus.

Fix

Added Math.max(0, streak) to ensure streak is non-negative before processing.

Testing

Added a new test case that verifies:

  • Negative streak values (-1, -100) return 0
  • Zero streak returns 0
  • Positive streaks work as expected (7 → 1, 14 → 2)

All 15 tests pass (including the new one).

Files Changed

  • common/src/util/freebuff-streak.ts - Added negative streak validation
  • common/src/util/__tests__/freebuff-streak.test.ts - Added test coverage

Scope

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

The function didn't validate that streak is non-negative. If streak was negative,
Math.floor(streak / INTERVAL) would be negative, and Math.min with the positive
max would return the negative value, resulting in a negative GLM bonus.

Added Math.max(0, streak) to ensure streak is non-negative before processing.

Also added a test case to verify the negative streak clamping behavior.
@codebuff-team

Copy link
Copy Markdown
Contributor

Good, focused fix. common/ is in scope and the change is minimal and low-risk.

The core question is whether streak can actually be negative at the call site — if callers always pass a validated non-negative streak, this is purely defensive. But defending internal utility functions against malformed input is reasonable, especially if streak could ever come from unsanitized user/session state or a miscalculated date diff (e.g. clock skew, timezone bugs) upstream. Worth checking whether calculateFreebuffStreak (or wherever the streak value that feeds this function originates) can ever produce a negative number — if so, this is a real bug fix; if not, it's cheap insurance either way.

The test added is clear and directly verifies the fix (-1, -100, 0, 7, 14). Nice touch keeping the existing test style consistent with the file.

One minor nit: consider using Math.max(0, Math.floor(safeStreak / ...)) reasoning is already handled correctly since Math.floor of a non-negative number stays non-negative — no issue there, just a note that the fix is correctly ordered (clamp before floor).

Small, well-scoped, has a test, easy to port. Recommend acceptance, though maintainers should double check whether negative streak is actually reachable in production to confirm this is fixing a real bug and not just a hypothetical.

@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 4, 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