refactor: Extract regex patterns into configurable arrays in extractSummary functions

Address code review feedback from Gemini Code Assist on PR #692.
Refactored the repeated matchAll() logic in extractSummary() functions to use
a loop over a configuration array, reducing code duplication and improving
maintainability.

- agent-context-parser.ts: Use regexesToTry array with group index
- log-parser.ts: Use regexesToTry array with processor functions for special handling

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Shirone
2026-01-25 12:24:57 +01:00
parent 35d2d41821
commit 3399d48823
2 changed files with 47 additions and 73 deletions

View File

@@ -243,40 +243,28 @@ function extractSummary(content: string): string | undefined {
// First, clean up any fragmented text from streaming // First, clean up any fragmented text from streaming
const cleanedContent = cleanFragmentedText(content); const cleanedContent = cleanFragmentedText(content);
// Look for <summary> tags - find ALL occurrences and take the LAST one // Define regex patterns to try in order of priority
const summaryTagMatches = [...cleanedContent.matchAll(/<summary>([\s\S]*?)<\/summary>/gi)]; // Each pattern specifies which capture group contains the summary content
if (summaryTagMatches.length > 0) { const regexesToTry = [
// Clean up the extracted summary content as well { regex: /<summary>([\s\S]*?)<\/summary>/gi, group: 1 },
return cleanFragmentedText(summaryTagMatches[summaryTagMatches.length - 1][1]).trim(); { regex: /## Summary[^\n]*\n([\s\S]*?)(?=\n## [^#]|\n🔧|$)/gi, group: 1 },
} {
regex:
// Fallback: Look for summary sections - find all and take the last /✓ (?:Feature|Verification|Task) (?:successfully|completed|verified)[^\n]*(?:\n[^\n]{1,200})?/gi,
// Stop at same-level ## sections (but not ###), or tool markers, or end group: 0,
const summaryMatches = [ },
...cleanedContent.matchAll(/## Summary[^\n]*\n([\s\S]*?)(?=\n## [^#]|\n🔧|$)/gi), {
regex: /(?:What was done|Changes made|Implemented)[^\n]*\n([\s\S]*?)(?=\n## [^#]|\n🔧|$)/gi,
group: 1,
},
]; ];
if (summaryMatches.length > 0) {
return cleanFragmentedText(summaryMatches[summaryMatches.length - 1][1]).trim();
}
// Look for completion markers and extract surrounding text - find all and take the last for (const { regex, group } of regexesToTry) {
const completionMatches = [ const matches = [...cleanedContent.matchAll(regex)];
...cleanedContent.matchAll( if (matches.length > 0) {
/✓ (?:Feature|Verification|Task) (?:successfully|completed|verified)[^\n]*(?:\n[^\n]{1,200})?/gi const lastMatch = matches[matches.length - 1];
), return cleanFragmentedText(lastMatch[group]).trim();
]; }
if (completionMatches.length > 0) {
return cleanFragmentedText(completionMatches[completionMatches.length - 1][0]).trim();
}
// Look for "What was done" type sections - find all and take the last
const whatWasDoneMatches = [
...cleanedContent.matchAll(
/(?:What was done|Changes made|Implemented)[^\n]*\n([\s\S]*?)(?=\n## [^#]|\n🔧|$)/gi
),
];
if (whatWasDoneMatches.length > 0) {
return cleanFragmentedText(whatWasDoneMatches[whatWasDoneMatches.length - 1][1]).trim();
} }
return undefined; return undefined;

View File

@@ -1211,49 +1211,35 @@ export function extractSummary(rawOutput: string): string | null {
// that got separated by newlines during accumulation (e.g., "<sum\n\nmary>") // that got separated by newlines during accumulation (e.g., "<sum\n\nmary>")
const cleanedOutput = cleanFragmentedText(rawOutput); const cleanedOutput = cleanFragmentedText(rawOutput);
// Try to find <summary> tags first (preferred format) // Define regex patterns to try in order of priority
// Use matchAll to find ALL occurrences and take the LAST one // Each pattern specifies a processor function to extract the summary from the match
const summaryTagMatches = [...cleanedOutput.matchAll(/<summary>([\s\S]*?)<\/summary>/gi)]; const regexesToTry: Array<{
if (summaryTagMatches.length > 0) { regex: RegExp;
// Clean up the extracted summary content as well processor: (m: RegExpMatchArray) => string;
return cleanFragmentedText(summaryTagMatches[summaryTagMatches.length - 1][1]).trim(); }> = [
} { regex: /<summary>([\s\S]*?)<\/summary>/gi, processor: (m) => m[1] },
{ regex: /^##\s+Summary[^\n]*\n([\s\S]*?)(?=\n##\s+[^#]|\n🔧|$)/gm, processor: (m) => m[1] },
// Try to find markdown ## Summary section - find all and take the last {
// Stop at same-level ## sections (but not ###), or tool markers, or end regex: /^##\s+(Feature|Changes|Implementation)[^\n]*\n([\s\S]*?)(?=\n##\s+[^#]|\n🔧|$)/gm,
const summaryHeaderMatches = [ processor: (m) => `## ${m[1]}\n${m[2]}`,
...cleanedOutput.matchAll(/^##\s+Summary[^\n]*\n([\s\S]*?)(?=\n##\s+[^#]|\n🔧|$)/gm), },
{
regex: /(^|\n)(All tasks completed[\s\S]*?)(?=\n🔧|\n📋|\n⚡|\n❌|$)/g,
processor: (m) => m[2],
},
{
regex:
/(^|\n)((I've|I have) (successfully |now )?(completed|finished|implemented)[\s\S]*?)(?=\n🔧|\n📋|\n⚡|\n❌|$)/g,
processor: (m) => m[2],
},
]; ];
if (summaryHeaderMatches.length > 0) {
return cleanFragmentedText(summaryHeaderMatches[summaryHeaderMatches.length - 1][1]).trim();
}
// Try other summary formats (Feature, Changes, Implementation) - find all and take the last for (const { regex, processor } of regexesToTry) {
const otherHeaderMatches = [ const matches = [...cleanedOutput.matchAll(regex)];
...cleanedOutput.matchAll( if (matches.length > 0) {
/^##\s+(Feature|Changes|Implementation)[^\n]*\n([\s\S]*?)(?=\n##\s+[^#]|\n🔧|$)/gm const lastMatch = matches[matches.length - 1];
), return cleanFragmentedText(processor(lastMatch)).trim();
]; }
if (otherHeaderMatches.length > 0) {
const lastMatch = otherHeaderMatches[otherHeaderMatches.length - 1];
return cleanFragmentedText(`## ${lastMatch[1]}\n${lastMatch[2]}`).trim();
}
// Try to find summary introduction lines - find all and take the last
const introMatches = [
...cleanedOutput.matchAll(/(^|\n)(All tasks completed[\s\S]*?)(?=\n🔧|\n📋|\n⚡|\n❌|$)/g),
];
if (introMatches.length > 0) {
return cleanFragmentedText(introMatches[introMatches.length - 1][2]).trim();
}
const completionMatches = [
...cleanedOutput.matchAll(
/(^|\n)((I've|I have) (successfully |now )?(completed|finished|implemented)[\s\S]*?)(?=\n🔧|\n📋|\n⚡|\n❌|$)/g
),
];
if (completionMatches.length > 0) {
return cleanFragmentedText(completionMatches[completionMatches.length - 1][2]).trim();
} }
return null; return null;