[GH-ISSUE #3017] [Issue]: spotify_set_volume sends volume_percent=NaN for non-numeric input instead of falling back to a valid value #1589

Closed
opened 2026-10-21 05:40:14 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @kolacheee on GitHub (Jun 29, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3017

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong

spotifySetVolume computes const volume = Math.max(0, Math.min(100, Number(args.volume ?? 50)));. The intent is that a missing/invalid volume falls back to 50 and otherwise clamps to [0,100]. But the clamp does not guard NaN: if args.volume is non-numeric, Number(args.volume) is NaN, Math.min(100, NaN) is NaN, and Math.max(0, NaN) is NaN. The NaN is interpolated into the request URL as volume_percent=NaN, which Spotify rejects with HTTP 400, so the tool returns an error instead of applying a sane default. (args.volume ?? 50 only rescues null/undefined, not a non-numeric string.)

How to reproduce (try it yourself)

  1. Call spotify_set_volume with { "volume": "loud" } (a non-numeric the model can emit, especially via text-mode tool-call parsing where args are not type-validated).

You'll see: Number("loud") → NaN; Math.max(0, Math.min(100, NaN)) → NaN; the request URL becomes https://api.spotify.com/v1/me/player/volume?volume_percent=NaN; Spotify returns 400; the tool returns { error: "Spotify volume failed (400): ..." }.

Expected: a non-numeric volume clamps to the 50 default and the volume is set, rather than producing volume_percent=NaN and a 400.

Where it is

packages/server/src/services/tools/tool-executor.ts:1785 (spotifySetVolume); the URL interpolation at line 1789. The arg-parse entry point with no validation is executeToolCalls at tool-executor.ts:190 (JSON.parse with no type coercion). The finite-guarded helper clampNumber is at lines 814-818.

What's happening

Volume normalization uses Math.max(0, Math.min(100, Number(...))) which does not handle NaN; an invalid Number() result passes through to the API call instead of using the clampNumber helper that falls back on non-finite input.

Suggested fix

Replace line 1785:

const volume = Math.max(0, Math.min(100, Number(args.volume ?? 50)));

with:

const volume = clampNumber(args.volume ?? 50, 50, 0, 100);

clampNumber (lines 814-818) guards non-finite results (if (!Number.isFinite(num)) return fallback;), so a non-numeric args.volume falls back to 50 instead of producing volume_percent=NaN. This matches the existing idiomatic call sites (e.g. line 1490, clampNumber(args.limit ?? 30, 30, 1, 50)).

Regression note: this is the source finding's exact suggested fix (no correction needed). clampNumber additionally applies Math.round, which is an improvement, not a regression — Spotify's volume_percent is an integer and the companion REST handler at packages/server/src/routes/spotify-auth.routes.ts:684 already rounds. The happy path (null/undefined → 50; valid numbers clamped to [0,100]) is preserved, and only the spotify_set_volume dispatch reaches this function.

Note: v2.0.7 (origin/staging) already applied this exact clampNumber fix to the sibling spotify_search and spotify_get_playlists limits (tool-executor.ts:780, 1531) but left spotify_set_volume unchanged — so this is a consistent, already-established follow-up.


AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.

Originally created by @kolacheee on GitHub (Jun 29, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3017 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** `spotifySetVolume` computes `const volume = Math.max(0, Math.min(100, Number(args.volume ?? 50)));`. The intent is that a missing/invalid volume falls back to 50 and otherwise clamps to [0,100]. But the clamp does not guard NaN: if `args.volume` is non-numeric, `Number(args.volume)` is NaN, `Math.min(100, NaN)` is NaN, and `Math.max(0, NaN)` is NaN. The NaN is interpolated into the request URL as `volume_percent=NaN`, which Spotify rejects with HTTP 400, so the tool returns an error instead of applying a sane default. (`args.volume ?? 50` only rescues null/undefined, not a non-numeric string.) **How to reproduce (try it yourself)** 1. Call `spotify_set_volume` with `{ "volume": "loud" }` (a non-numeric the model can emit, especially via text-mode tool-call parsing where args are not type-validated). You'll see: `Number("loud")` → NaN; `Math.max(0, Math.min(100, NaN))` → NaN; the request URL becomes `https://api.spotify.com/v1/me/player/volume?volume_percent=NaN`; Spotify returns 400; the tool returns `{ error: "Spotify volume failed (400): ..." }`. Expected: a non-numeric volume clamps to the 50 default and the volume is set, rather than producing `volume_percent=NaN` and a 400. **Where it is** `packages/server/src/services/tools/tool-executor.ts:1785` (`spotifySetVolume`); the URL interpolation at line 1789. The arg-parse entry point with no validation is `executeToolCalls` at `tool-executor.ts:190` (`JSON.parse` with no type coercion). The finite-guarded helper `clampNumber` is at lines 814-818. **What's happening** Volume normalization uses `Math.max(0, Math.min(100, Number(...)))` which does not handle NaN; an invalid `Number()` result passes through to the API call instead of using the `clampNumber` helper that falls back on non-finite input. **Suggested fix** Replace line 1785: ```ts const volume = Math.max(0, Math.min(100, Number(args.volume ?? 50))); ``` with: ```ts const volume = clampNumber(args.volume ?? 50, 50, 0, 100); ``` `clampNumber` (lines 814-818) guards non-finite results (`if (!Number.isFinite(num)) return fallback;`), so a non-numeric `args.volume` falls back to 50 instead of producing `volume_percent=NaN`. This matches the existing idiomatic call sites (e.g. line 1490, `clampNumber(args.limit ?? 30, 30, 1, 50)`). *Regression note:* this is the source finding's exact suggested fix (no correction needed). `clampNumber` additionally applies `Math.round`, which is an improvement, not a regression — Spotify's `volume_percent` is an integer and the companion REST handler at `packages/server/src/routes/spotify-auth.routes.ts:684` already rounds. The happy path (null/undefined → 50; valid numbers clamped to [0,100]) is preserved, and only the `spotify_set_volume` dispatch reaches this function. *Note: v2.0.7 (origin/staging) already applied this exact `clampNumber` fix to the sibling `spotify_search` and `spotify_get_playlists` limits (tool-executor.ts:780, 1531) but left `spotify_set_volume` unchanged — so this is a consistent, already-established follow-up.* --- *AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.*
bsakaguchi 2026-10-21 05:40:14 +07:00
Author
Owner

@SpicyMarinara commented on GitHub (Jun 29, 2026):

Fixed in merged staging PR #3018.

<!-- gh-comment-id:4833020317 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Fixed in merged staging PR #3018.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#1589
No description provided.