-
Notifications
You must be signed in to change notification settings - Fork 43
fix(auto-triage): avoid regex code fence parsing #3854
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -99,13 +99,7 @@ const parseFromCodeBlock = ( | |||||
| text: string, | ||||||
| availableLabels: string[] | ||||||
| ): ClassificationResult | null => { | ||||||
| const codeBlockRegex = /```(?:json|JSON)?\s*\r?\n([\s\S]*?)\r?\n\s*```/g; | ||||||
| const codeBlocks: string[] = []; | ||||||
| let match; | ||||||
|
|
||||||
| while ((match = codeBlockRegex.exec(text)) !== null) { | ||||||
| codeBlocks.push(match[1]); | ||||||
| } | ||||||
| const codeBlocks = extractCodeBlocks(text); | ||||||
|
|
||||||
| // Try code blocks from last to first (most recent) | ||||||
| for (let i = codeBlocks.length - 1; i >= 0; i--) { | ||||||
|
|
@@ -119,8 +113,7 @@ const parseFromCodeBlock = ( | |||||
| } | ||||||
|
|
||||||
| // Fallback: direct tail search for the last code fence pair. | ||||||
| // The regex with lazy quantifier can miss the final block in very large texts | ||||||
| // with many ``` markers, so search backwards from the end instead. | ||||||
| // Search backwards from the end to recover from unmatched earlier fences. | ||||||
| const lastFenceEnd = text.lastIndexOf('```'); | ||||||
| if (lastFenceEnd !== -1) { | ||||||
| const searchStart = Math.max(0, lastFenceEnd - 10_000); | ||||||
|
|
@@ -145,6 +138,57 @@ const parseFromCodeBlock = ( | |||||
| return null; | ||||||
| }; | ||||||
|
|
||||||
| const extractCodeBlocks = (text: string): string[] => { | ||||||
| const codeBlocks: string[] = []; | ||||||
| let searchIndex = 0; | ||||||
|
|
||||||
| while (searchIndex < text.length) { | ||||||
| const openFenceIndex = text.indexOf('```', searchIndex); | ||||||
| if (openFenceIndex === -1) break; | ||||||
|
|
||||||
| const infoStartIndex = openFenceIndex + 3; | ||||||
| const contentStartIndex = text.indexOf('\n', infoStartIndex); | ||||||
| if (contentStartIndex === -1) break; | ||||||
|
|
||||||
| const fenceInfo = text.substring(infoStartIndex, contentStartIndex).trim(); | ||||||
| if (fenceInfo !== '' && fenceInfo !== 'json' && fenceInfo !== 'JSON') { | ||||||
| searchIndex = contentStartIndex + 1; | ||||||
| continue; | ||||||
| } | ||||||
|
|
||||||
| const closeFenceLineIndex = findClosingFenceLine(text, contentStartIndex + 1); | ||||||
| if (closeFenceLineIndex === -1) { | ||||||
| searchIndex = contentStartIndex + 1; | ||||||
| continue; | ||||||
| } | ||||||
|
|
||||||
| codeBlocks.push(text.substring(contentStartIndex + 1, closeFenceLineIndex)); | ||||||
| const closeFenceLineEndIndex = text.indexOf('\n', closeFenceLineIndex); | ||||||
| searchIndex = closeFenceLineEndIndex === -1 ? text.length : closeFenceLineEndIndex + 1; | ||||||
| } | ||||||
|
|
||||||
| return codeBlocks; | ||||||
| }; | ||||||
|
|
||||||
| const findClosingFenceLine = (text: string, startIndex: number): number => { | ||||||
| let lineStartIndex = startIndex; | ||||||
|
|
||||||
| while (lineStartIndex < text.length) { | ||||||
| const lineEndIndex = text.indexOf('\n', lineStartIndex); | ||||||
| const line = text.substring(lineStartIndex, lineEndIndex === -1 ? text.length : lineEndIndex); | ||||||
| const leadingWhitespaceLength = line.length - line.trimStart().length; | ||||||
|
|
||||||
| if (line.startsWith('```', leadingWhitespaceLength)) { | ||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WARNING: The check The old regex implicitly required the closing fence to consist of only backticks (possibly with surrounding whitespace). The fix is to require the remainder of the line after the backticks to be empty:
Suggested change
|
||||||
| return lineStartIndex; | ||||||
| } | ||||||
|
|
||||||
| if (lineEndIndex === -1) break; | ||||||
| lineStartIndex = lineEndIndex + 1; | ||||||
| } | ||||||
|
|
||||||
| return -1; | ||||||
| }; | ||||||
|
|
||||||
| /** | ||||||
| * Extract classification from plain JSON objects in text | ||||||
| * Uses balanced brace matching to find JSON objects | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WARNING: Windows
\r\nline endings are not stripped — extracted JSON content will contain trailing\ron every line, breakingJSON.parse.The old regex used
\r?\nat both boundaries to strip carriage returns. Here,text.substring(..., lineEndIndex)keeps the\rbefore the\n, so when the extracted block content is passed toJSON.parseit will fail on Windows-generated or mixed-EOL text.Fix: strip the trailing
\rfrom each line: