different-ai/openwork · error · InterpreterRuntimeError

RegExp method '${name}' is not available in CodeMode.

Error message

RegExp method '${name}' is not available in CodeMode.

What it means

CodeMode exposes only a whitelist of RegExp instance methods (test, exec, toString). Any other member invoked on a RegExp value — e.g. compile or Symbol-based methods — is rejected by design to keep the sandbox surface small and auditable.

Source

Thrown at packages/codemode/src/stdlib/regexp.ts:68

}

export const invokeRegExpMethod = (
  value: SandboxRegExp,
  name: string,
  args: Array<unknown>,
  node: AstNode,
): unknown => {
  switch (name) {
    case "test":
      return value.regex.test(coerceToString(args[0]))
    case "exec": {
      const matched = value.regex.exec(coerceToString(args[0]))
      return matched === null ? null : matchToValue(matched)
    }
    case "toString":
      return coerceToString(value)
    default:
      throw new InterpreterRuntimeError(`RegExp method '${name}' is not available in CodeMode.`, node)
  }
}
import { type AstNode, InterpreterRuntimeError } from "../interpreter/model.js"
import { isBlockedMember, type SafeObject } from "../tool-runtime.js"
import { SandboxRegExp } from "../values.js"
import { coerceToString } from "./value.js"

View on GitHub (pinned to 2b7df46e8a)

Solutions

  1. Rewrite the logic using only the allowed methods: test, exec, toString.
  2. Recreate the pattern with new RegExp instead of calling compile().
  3. Use string methods (match/replace/split/search) with the pattern instead.

Example fix

// before
const re = /a/g
re.compile("b")
// after
const re = new RegExp("b", "g")
const ok = re.test("abc")
Defensive patterns

Strategy: validation

Validate before calling

const allowed = new Set(["test", "exec", "toString"])
if (!allowed.has(methodName)) console.warn(`use only ${[...allowed]} on RegExp in CodeMode`)

Type guard

const isAllowedRegExpMethod = (name) => ["test", "exec", "toString"].includes(name)

Try / catch

try { r = regex.compile(p) } catch { r = new RegExp(p) }

Prevention

When it happens

Trigger: Calling `regex.compile(...)` or any non-whitelisted method on a value produced by a /pattern/ literal or `new RegExp(...)` inside CodeMode.

Common situations: Porting regular JS code that uses full RegExp APIs into CodeMode; forgetting that the sandbox intentionally omits rarely-used RegExp methods.

Related errors


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