feat: Update summary extraction logic to return the most recent summary from multiple occurrences

- Enhanced `extractSummary` functions in `agent-context-parser.ts` and `log-parser.ts` to utilize `matchAll` for capturing all summary instances.
- Modified logic to return the last found summary, ensuring the most recent content is extracted.
- Improved handling of fragmented text and various summary formats for consistency.
This commit is contained in:
Shirone
2026-01-25 12:15:05 +01:00
parent 7a5cb38a37
commit 35d2d41821
2 changed files with 68 additions and 45 deletions

View File

@@ -237,39 +237,46 @@ function cleanFragmentedText(content: string): string {
/** /**
* Extracts a summary from completed feature context * Extracts a summary from completed feature context
* Looks for content between <summary> and </summary> tags * Looks for content between <summary> and </summary> tags
* Returns the LAST summary found to ensure we get the most recent/updated one
*/ */
function extractSummary(content: string): string | undefined { 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 - capture everything between opening and closing tags // Look for <summary> tags - find ALL occurrences and take the LAST one
const summaryTagMatch = cleanedContent.match(/<summary>([\s\S]*?)<\/summary>/i); const summaryTagMatches = [...cleanedContent.matchAll(/<summary>([\s\S]*?)<\/summary>/gi)];
if (summaryTagMatch) { if (summaryTagMatches.length > 0) {
// Clean up the extracted summary content as well // Clean up the extracted summary content as well
return cleanFragmentedText(summaryTagMatch[1]).trim(); return cleanFragmentedText(summaryTagMatches[summaryTagMatches.length - 1][1]).trim();
} }
// Fallback: Look for summary sections - capture everything including subsections (###) // Fallback: Look for summary sections - find all and take the last
// Stop at same-level ## sections (but not ###), or tool markers, or end // Stop at same-level ## sections (but not ###), or tool markers, or end
const summaryMatch = cleanedContent.match(/## Summary[^\n]*\n([\s\S]*?)(?=\n## [^#]|\n🔧|$)/i); const summaryMatches = [
if (summaryMatch) { ...cleanedContent.matchAll(/## Summary[^\n]*\n([\s\S]*?)(?=\n## [^#]|\n🔧|$)/gi),
return cleanFragmentedText(summaryMatch[1]).trim(); ];
if (summaryMatches.length > 0) {
return cleanFragmentedText(summaryMatches[summaryMatches.length - 1][1]).trim();
} }
// Look for completion markers and extract surrounding text // Look for completion markers and extract surrounding text - find all and take the last
const completionMatch = cleanedContent.match( const completionMatches = [
/✓ (?:Feature|Verification|Task) (?:successfully|completed|verified)[^\n]*(?:\n[^\n]{1,200})?/i ...cleanedContent.matchAll(
); /✓ (?:Feature|Verification|Task) (?:successfully|completed|verified)[^\n]*(?:\n[^\n]{1,200})?/gi
if (completionMatch) { ),
return cleanFragmentedText(completionMatch[0]).trim(); ];
if (completionMatches.length > 0) {
return cleanFragmentedText(completionMatches[completionMatches.length - 1][0]).trim();
} }
// Look for "What was done" type sections // Look for "What was done" type sections - find all and take the last
const whatWasDoneMatch = cleanedContent.match( const whatWasDoneMatches = [
/(?:What was done|Changes made|Implemented)[^\n]*\n([\s\S]*?)(?=\n## [^#]|\n🔧|$)/i ...cleanedContent.matchAll(
); /(?:What was done|Changes made|Implemented)[^\n]*\n([\s\S]*?)(?=\n## [^#]|\n🔧|$)/gi
if (whatWasDoneMatch) { ),
return cleanFragmentedText(whatWasDoneMatch[1]).trim(); ];
if (whatWasDoneMatches.length > 0) {
return cleanFragmentedText(whatWasDoneMatches[whatWasDoneMatches.length - 1][1]).trim();
} }
return undefined; return undefined;

View File

@@ -1198,46 +1198,62 @@ function mergeConsecutiveEntries(entries: LogEntry[]): LogEntry[] {
/** /**
* Extracts summary content from raw log output * Extracts summary content from raw log output
* Returns the summary text if found, or null if no summary exists * Returns the LAST summary text if found, or null if no summary exists
* This ensures we get the most recent/updated summary when multiple exist
*/ */
export function extractSummary(rawOutput: string): string | null { export function extractSummary(rawOutput: string): string | null {
if (!rawOutput || !rawOutput.trim()) { if (!rawOutput || !rawOutput.trim()) {
return null; return null;
} }
// First, clean up any fragmented text from streaming
// This handles cases where streaming providers send partial text chunks
// that got separated by newlines during accumulation (e.g., "<sum\n\nmary>")
const cleanedOutput = cleanFragmentedText(rawOutput);
// Try to find <summary> tags first (preferred format) // Try to find <summary> tags first (preferred format)
const summaryTagMatch = rawOutput.match(/<summary>([\s\S]*?)<\/summary>/); // Use matchAll to find ALL occurrences and take the LAST one
if (summaryTagMatch) { const summaryTagMatches = [...cleanedOutput.matchAll(/<summary>([\s\S]*?)<\/summary>/gi)];
return summaryTagMatch[1].trim(); if (summaryTagMatches.length > 0) {
// Clean up the extracted summary content as well
return cleanFragmentedText(summaryTagMatches[summaryTagMatches.length - 1][1]).trim();
} }
// Try to find markdown ## Summary section // Try to find markdown ## Summary section - find all and take the last
const summaryHeaderMatch = rawOutput.match(/^##\s+Summary\s*\n([\s\S]*?)(?=\n##\s+|$)/m); // Stop at same-level ## sections (but not ###), or tool markers, or end
if (summaryHeaderMatch) { const summaryHeaderMatches = [
return summaryHeaderMatch[1].trim(); ...cleanedOutput.matchAll(/^##\s+Summary[^\n]*\n([\s\S]*?)(?=\n##\s+[^#]|\n🔧|$)/gm),
];
if (summaryHeaderMatches.length > 0) {
return cleanFragmentedText(summaryHeaderMatches[summaryHeaderMatches.length - 1][1]).trim();
} }
// Try other summary formats (Feature, Changes, Implementation) // Try other summary formats (Feature, Changes, Implementation) - find all and take the last
const otherHeaderMatch = rawOutput.match( const otherHeaderMatches = [
/^##\s+(Feature|Changes|Implementation)\s*\n([\s\S]*?)(?=\n##\s+|$)/m ...cleanedOutput.matchAll(
); /^##\s+(Feature|Changes|Implementation)[^\n]*\n([\s\S]*?)(?=\n##\s+[^#]|\n🔧|$)/gm
if (otherHeaderMatch) { ),
return `## ${otherHeaderMatch[1]}\n${otherHeaderMatch[2].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 // Try to find summary introduction lines - find all and take the last
const introMatch = rawOutput.match( const introMatches = [
/(^|\n)(All tasks completed[\s\S]*?)(?=\n🔧|\n📋|\n⚡|\n❌|$)/ ...cleanedOutput.matchAll(/(^|\n)(All tasks completed[\s\S]*?)(?=\n🔧|\n📋|\n⚡|\n❌|$)/g),
); ];
if (introMatch) { if (introMatches.length > 0) {
return introMatch[2].trim(); return cleanFragmentedText(introMatches[introMatches.length - 1][2]).trim();
} }
const completionMatch = rawOutput.match( const completionMatches = [
/(^|\n)((I've|I have) (successfully |now )?(completed|finished|implemented)[\s\S]*?)(?=\n🔧|\n📋|\n⚡|\n❌|$)/ ...cleanedOutput.matchAll(
); /(^|\n)((I've|I have) (successfully |now )?(completed|finished|implemented)[\s\S]*?)(?=\n🔧|\n📋|\n⚡|\n❌|$)/g
if (completionMatch) { ),
return completionMatch[2].trim(); ];
if (completionMatches.length > 0) {
return cleanFragmentedText(completionMatches[completionMatches.length - 1][2]).trim();
} }
return null; return null;