Skip to content

Conversation

@kevintraver
Copy link
Contributor

Summary

  • Fix crash when log lines contain regex metacharacters (brackets, parentheses, etc.)
  • Add fallback to substring matching if regex construction fails

Problem

The calculateErrorPriority function in mcp-server/app/mcp/tools.ts creates a regex pattern from error lines to count how many similar errors have occurred (used to boost priority for recurring errors). The pattern is built by replacing digit sequences with \d+ so that errors differing only in timestamps or line numbers are grouped together.

However, log lines commonly contain characters that have special meaning in regex:

  • Timestamps: [12:34:56.789] — square brackets define character classes
  • Context info: (connection closed) — parentheses define capture groups
  • File paths: C:\Users\... — backslashes are escape characters
  • Assertions: expected 5, got 10 — could contain +, *, ?

When these unescaped characters were passed to new RegExp(), it would throw errors like:

Invalid regular expression: Unterminated group
Invalid regular expression: Unterminated character class

This crashed the error analysis flow entirely.

Solution

  1. Escape regex metacharacters first: Before replacing digits with \d+, escape all special regex characters ([.*+?^${}()|[\]\\]) using the standard \\$& replacement pattern
  2. Add defensive try/catch: If regex construction still fails for an unexpected reason, fall back to simple substring matching (includes()) rather than crashing
// Before (crashed on brackets/parens)
const errorPattern = cleanLine.replace(/\d+/g, "\\d+").substring(0, 100)
const occurrences = allErrors.filter((e) => new RegExp(errorPattern).test(e)).length

// After (safe)
const escapedLine = cleanLine.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
const errorPattern = escapedLine.replace(/\d+/g, "\\d+").substring(0, 100)
let occurrences = 0
try {
  occurrences = allErrors.filter((e) => new RegExp(errorPattern).test(e)).length
} catch {
  occurrences = allErrors.filter((e) => e.includes(cleanLine.substring(0, 50))).length
}

Test plan

  • Build canary and test with logs containing brackets/parentheses
  • Verify error priority calculation still works for normal error lines

🤖 Generated with Claude Code

Log lines containing regex metacharacters like brackets and parentheses
(e.g., timestamps `[12:34:56.789]` or context info `(co...`) were causing
"Invalid regular expression: Unterminated group" errors when used to
count similar error occurrences.

Now escapes all regex special characters before replacing digits with
`\d+`, and adds a try/catch fallback to substring matching if regex
construction still fails.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@vercel
Copy link
Contributor

vercel bot commented Jan 16, 2026

@kevintraver is attempting to deploy a commit to the Vercel Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant