can1357/oh-my-pi · error · ToolError
Invalid regex: ${err.message}
Error message
Invalid regex: ${err.message} What it means
The grep engine rejected the pattern because it is not valid regular expression syntax (e.g. unbalanced parenthesis, dangling quantifier, invalid escape). The tool catches the underlying regex parse error and rewrites it as "Invalid regex: ..." so the cause is clear.
Source
Thrown at packages/coding-agent/src/tools/grep.ts:1241
hidden: true,
gitignore: useGitignore,
maxCount: nativeMaxCount,
contextBefore: normalizedContextBefore,
contextAfter: normalizedContextAfter,
maxColumns: DEFAULT_MAX_COLUMN,
mode: effectiveOutputMode,
maxCountPerFile: nativeMaxCountPerFile,
signal,
timeoutMs: SEARCH_GREP_TIMEOUT_MS,
},
undefined,
);
skippedOversizedCount = result.skippedOversized ?? 0;
}
}
} catch (err) {
if (err instanceof Error && /^regex(?: parse)? error/i.test(err.message)) {
throw new ToolError(err.message.replace(/^regex(?: parse)? error:?\s*/i, "Invalid regex: "));
}
if (err instanceof Error && err.message.includes("Aborted: Timeout")) {
throw new ToolError(
`Grep timed out after ${SEARCH_GREP_TIMEOUT_MS / 1000}s; narrow paths or pattern, or scope with \`glob\` first`,
);
}
throw err;
}
let virtualResult: GrepResult;
try {
virtualResult = await searchVirtualResources(
virtualResources,
normalizedPattern,
ignoreCase,
effectiveMultiline,
normalizedContextBefore,
normalizedContextAfter,
INTERNAL_TOTAL_CAP,View on GitHub (pinned to 9690622007)
Solutions
- Fix the regex syntax (balance parens/brackets, escape metacharacters with \\)
- For literal text search, escape metacharacters: const esc = s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
- Check for unsupported constructs (backreferences/lookaround may be unsupported by the engine)
Example fix
// before
await grep({ pattern: `call_${userInput}(` });
// after
const esc = userInput.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
await grep({ pattern: `call_${esc}\\(` }); Defensive patterns
Strategy: validation
Validate before calling
try { new RegExp(pattern); } catch (e) { throw new Error(`Invalid grep regex: ${e.message}`); } Type guard
const isValidRegex = (p: string): boolean => { try { new RegExp(p); return true; } catch { return false; } }; Try / catch
try {
await grep({ pattern });
} catch (err) {
if (err instanceof ToolError && err.message.startsWith("Invalid regex:")) {
// escape metacharacters and retry as literal
} else throw err;
} Prevention
- Escape user text with a metacharacter escaper before building regex patterns
- Test patterns in a regex validator, noting engine-specific syntax limits
When it happens
Trigger: grep with patterns like "foo(", "a{2,1}", "\\", or "[a-"; patterns built by string interpolation that accidentally include regex metacharacters.
Common situations: Searching for literal text containing (, ), [, +, or ? without escaping; user input passed directly as a regex; Rust-regex vs JS regex syntax differences in lookbehind/backreferences.
Related errors
- err.to_string() (invalid regex pattern)
- Invalid log regex: ${error instanceof Error ? error.message
- Invalid readiness regex: ${error instanceof Error ? error.me
- Invalid wait regex: ${error instanceof Error ? error.message
- Pattern must not be empty
AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31).
Data as JSON: /api/errors/5c387e4f24ada3eb.
Report an issue: GitHub.