Skip to content

refactor: deduplicate clampInt utility across convex modules#1533

Open
goulonghui wants to merge 1 commit intoopenclaw:mainfrom
goulonghui:refactor/deduplicate-clamp-int
Open

refactor: deduplicate clampInt utility across convex modules#1533
goulonghui wants to merge 1 commit intoopenclaw:mainfrom
goulonghui:refactor/deduplicate-clamp-int

Conversation

@goulonghui
Copy link
Copy Markdown

Extract the clampInt helper into convex/lib/math.ts and replace 11 duplicate definitions across the codebase. The previous copies used five different rounding strategies (Math.round, Math.trunc, Math.floor, Number.isFinite guard, or none). The canonical version uses Math.trunc with a Number.isFinite guard, matching the most defensive variant from maintenance.ts.

Files updated:

  • convex/souls.ts
  • convex/commentModeration.ts
  • convex/leaderboards.ts
  • convex/users.ts
  • convex/statsMaintenance.ts
  • convex/githubBackups.ts
  • convex/githubBackupsNode.ts
  • convex/githubSoulBackups.ts
  • convex/githubSoulBackupsNode.ts
  • convex/maintenance.ts
  • convex/skills.ts

Extract the clampInt helper into convex/lib/math.ts and replace 11
duplicate definitions across the codebase.  The previous copies used
five different rounding strategies (Math.round, Math.trunc, Math.floor,
Number.isFinite guard, or none).  The canonical version uses Math.trunc
with a Number.isFinite guard, matching the most defensive variant from
maintenance.ts.

Files updated:
  - convex/souls.ts
  - convex/commentModeration.ts
  - convex/leaderboards.ts
  - convex/users.ts
  - convex/statsMaintenance.ts
  - convex/githubBackups.ts
  - convex/githubBackupsNode.ts
  - convex/githubSoulBackups.ts
  - convex/githubSoulBackupsNode.ts
  - convex/maintenance.ts
  - convex/skills.ts
@vercel
Copy link
Copy Markdown
Contributor

vercel bot commented Apr 5, 2026

Someone is attempting to deploy a commit to the 0xBuns Team on Vercel.

A member of the Team first needs to authorize it.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Apr 5, 2026

Greptile Summary

This refactor consolidates 11 duplicate clampInt definitions into convex/lib/math.ts with a test suite. The five previously divergent implementations (Math.round, Math.trunc, Math.floor, no rounding, or no finite guard) are unified under Math.trunc + Number.isFinite guard — the most defensive variant from maintenance.ts — but the prior implementations in skills.ts and souls.ts used Math.round, silently changing how non-integer pagination limits are clamped for those two modules.

Confidence Score: 4/5

Safe to merge if the Math.round → Math.trunc behavior change in skills.ts and souls.ts is intentional.

One silent semantic change (Math.round → Math.trunc) affects pagination limits in skills.ts and souls.ts. In practice callers almost certainly pass integer limits, but the Convex API accepts any float and the change is undocumented.

convex/skills.ts and convex/souls.ts — confirm the switch from Math.round to Math.trunc is intentional for their pagination limit call sites.

Prompt To Fix All With AI
This is a comment left during a code review.
Path: convex/lib/math.ts
Line: 8

Comment:
**Silent `Math.round``Math.trunc` behavior change for skills.ts and souls.ts**

The old `clampInt` in `convex/skills.ts` and `convex/souls.ts` used `Math.round`, so a float like `3.9` would produce `4`; `Math.trunc(3.9)` returns `3`. All call sites in those two files are pagination limits (`args.limit`, `args.numItems`) backed by `v.optional(v.number())` validators — meaning any float is a valid input. A caller passing `limit: 5.9` previously received up to 6 results and will now receive 5. Please confirm the switch to `Math.trunc` is intentional for these modules, or restore rounding at the affected call sites.

How can I resolve this? If you propose a fix, please make it concise.

Reviews (1): Last reviewed commit: "refactor: deduplicate clampInt utility a..." | Re-trigger Greptile

* falling back to `min`, and clamps the result to the given range.
*/
export function clampInt(value: number, min: number, max: number): number {
const truncated = Math.trunc(value);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Silent Math.roundMath.trunc behavior change for skills.ts and souls.ts

The old clampInt in convex/skills.ts and convex/souls.ts used Math.round, so a float like 3.9 would produce 4; Math.trunc(3.9) returns 3. All call sites in those two files are pagination limits (args.limit, args.numItems) backed by v.optional(v.number()) validators — meaning any float is a valid input. A caller passing limit: 5.9 previously received up to 6 results and will now receive 5. Please confirm the switch to Math.trunc is intentional for these modules, or restore rounding at the affected call sites.

Prompt To Fix With AI
This is a comment left during a code review.
Path: convex/lib/math.ts
Line: 8

Comment:
**Silent `Math.round``Math.trunc` behavior change for skills.ts and souls.ts**

The old `clampInt` in `convex/skills.ts` and `convex/souls.ts` used `Math.round`, so a float like `3.9` would produce `4`; `Math.trunc(3.9)` returns `3`. All call sites in those two files are pagination limits (`args.limit`, `args.numItems`) backed by `v.optional(v.number())` validators — meaning any float is a valid input. A caller passing `limit: 5.9` previously received up to 6 results and will now receive 5. Please confirm the switch to `Math.trunc` is intentional for these modules, or restore rounding at the affected call sites.

How can I resolve this? If you propose a fix, please make it concise.

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.

1 participant