different-ai/openwork · error · InterpreterRuntimeError

String.repeat expects a finite non-negative count.

Error message

String.repeat expects a finite non-negative count.

What it means

String.repeat's count argument is validated beyond typeof: it must be a finite number >= 0. The host throws RangeError for negative or infinite counts (and for results exceeding max string length); the sandbox throws its own InterpreterRuntimeError first with a clearer message. NaN also fails the Number.isFinite check.

Source

Thrown at packages/codemode/src/interpreter/runtime.ts:469

      const pattern = toHostRegex(args[0], name, node, "g")
      if (!pattern.global) {
        throw new InterpreterRuntimeError(
          `String.matchAll requires a regular expression with the global (g) flag: write /${pattern.source}/${pattern.flags}g, or use String.match for a single match.`,
          node,
        )
      }
      // Materialized as an array (not an iterator); each entry is a match array with
      // index/groups own properties. Match count is bounded by the subject length.
      return Array.from(value.matchAll(pattern), matchToValue)
    }
    case "search": {
      result = value.search(toHostRegex(args[0], name, node))
      break
    }
    case "repeat": {
      const count = num(0)
      if (!Number.isFinite(count) || count < 0)
        throw new InterpreterRuntimeError("String.repeat expects a finite non-negative count.", node)
      result = value.repeat(count)
      break
    }
    case "padStart":
      result = value.padStart(num(0), optStr(1))
      break
    case "padEnd":
      result = value.padEnd(num(0), optStr(1))
      break
    case "charAt":
      result = value.charAt(optNum(0) ?? 0)
      break
    case "at":
      result = value.at(optNum(0) ?? 0)
      break
    case "substring":
      result = value.substring(optNum(0) ?? 0, optNum(1))
      break

View on GitHub (pinned to 2b7df46e8a)

Solutions

  1. Clamp the count: Math.max(0, Math.floor(count)) before calling repeat
  2. Validate the computed count with Number.isFinite(n) && n >= 0 before the call
  3. Check the source of the count for NaN (failed numeric parse) and negative diffs (swapped bounds)
  4. Guard empty-input cases that make the count negative

Example fix

// before
const pad = " ".repeat(width - text.length)
// after
const count = Math.max(0, Math.floor(width - text.length))
const pad = " ".repeat(count)
Defensive patterns

Strategy: validation

Validate before calling

function safeCount(n) { const c = Number(n); if (!Number.isFinite(c) || c < 0) throw new TypeError(`repeat count must be a finite non-negative number, got ${n}`); return Math.floor(c) }
"-".repeat(safeCount(n))

Type guard

const isValidCount = (v) => typeof v === "number" && Number.isFinite(v) && v >= 0

Try / catch

try { out = s.repeat(n) } catch (e) { if (String(e.message).includes("finite non-negative")) { n = Math.max(0, Math.floor(Number(n) || 0)); out = s.repeat(n) } else throw e }

Prevention

When it happens

Trigger: Calling str.repeat(-1), str.repeat(Infinity), str.repeat(NaN), or a computed count like str.repeat(n - m) where the subtraction went negative; repeat with a count derived from user input or an empty array length unexpectedly 0 is fine, but a negative computed offset is common.

Common situations: Repeat counts computed from differences (end - start) with inverted bounds; unvalidated user-specified repetition counts; NaN from parseFloat of bad input; off-by-one making the count -1 on empty input.

Related errors


AI-assisted analysis of different-ai/openwork@2b7df46e8a (2026-09-01). Data as JSON: /api/errors/33f7a07bd3e392e0. Report an issue: GitHub.