{"record":{"id":"ebfaae0ef06e5ee0","repo":"different-ai/openwork","slug":"math-name-expects-number-arguments","errorCode":null,"errorMessage":"Math.${name} expects number arguments.","messagePattern":"Math\\.(.+?) expects number arguments\\.","errorType":"exception","errorClass":"InterpreterRuntimeError","httpStatus":null,"severity":"error","filePath":"packages/codemode/src/stdlib/math.ts","lineNumber":25,"sourceCode":"  \"floor\",\n  \"ceil\",\n  \"round\",\n  \"trunc\",\n  \"sign\",\n  \"sqrt\",\n  \"cbrt\",\n  \"pow\",\n  \"hypot\",\n  \"log\",\n  \"log2\",\n  \"log10\",\n  \"exp\",\n])\n\nexport const invokeMathMethod = (name: string, args: Array<unknown>, node: AstNode): number => {\n  if (!mathMethods.has(name)) throw new InterpreterRuntimeError(`Math.${name} is not available in CodeMode.`, node)\n  const nums = args.map((arg) => {\n    if (typeof arg !== \"number\") throw new InterpreterRuntimeError(`Math.${name} expects number arguments.`, node)\n    return arg\n  })\n  const [a = Number.NaN, b = Number.NaN] = nums\n  switch (name) {\n    case \"max\":\n      return Math.max(...nums)\n    case \"min\":\n      return Math.min(...nums)\n    case \"abs\":\n      return Math.abs(a)\n    case \"floor\":\n      return Math.floor(a)\n    case \"ceil\":\n      return Math.ceil(a)\n    case \"round\":\n      return Math.round(a)\n    case \"trunc\":\n      return Math.trunc(a)","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/different-ai/openwork/blob/2b7df46e8ae1517d64c896c7793d2d52ec845669/packages/codemode/src/stdlib/math.ts#L7-L43","documentation":"Once the method name passes the whitelist check, CodeMode validates that every argument is a number. Non-numeric arguments (strings, NaN-producing values from upstream, undefined) are rejected with this error instead of being silently coerced, keeping sandbox math strict and predictable.","triggerScenarios":"Calling any supported Math method with a non-number argument: Math.pow(\"2\", 3), Math.max(...arr) where arr contains undefined, Math.log2(values[0]) where the array slot is missing.","commonSituations":"Spreading arrays that may contain holes or undefined entries (Math.max(...sparse)); passing numeric strings from JSON input without Number() conversion; downstream functions returning undefined on edge cases.","solutions":["Coerce inputs explicitly with Number(x) before passing them to Math methods.","Filter the array before spreading: nums.filter(n => typeof n === \"number\").","Guard optional/missing values with a default before the call."],"exampleFix":"// before\nconst m = Math.max(...values)\n// after\nconst m = Math.max(...values.filter((n) => typeof n === \"number\"))","handlingStrategy":"type-guard","validationCode":"const nums = args.filter((a) => typeof a === \"number\")\nif (nums.length !== args.length) throw new TypeError(\"Math arguments must all be numbers\")","typeGuard":"const isNum = (v: unknown): v is number => typeof v === \"number\" && Number.isFinite(v)","tryCatchPattern":"try { return Math.max(...values) } catch (e) { if (/expects number arguments/.test(String(e))) return Math.max(...values.filter(isNum)); throw e }","preventionTips":["Coerce config/JSON-sourced values with Number() before math calls","Filter arrays for typeof number before spreading into Math.min/max","Guard optional array slots with defaults before use"],"tags":["math","codemode","type-error","argument-validation"],"backgroundTag":"invalid-function-arguments","analyzedSha":"2b7df46e8ae1517d64c896c7793d2d52ec845669","analyzedAt":"2026-09-01T07:59:23.713Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}