Skip to content

Commit b119911

Browse files
elsighclaude
andcommitted
Add fallback to parse CLS score from d3k logs when metadata unavailable
When the metadata endpoint fails or returns no totalCLS, we now parse the CLS score directly from d3k logs that contain lines like: "Layout shift detected (element: DIV, position: static, score: 0.4716)" This ensures workflow reports capture the correct initial CLS score even when the screencast metadata JSON endpoint is unavailable. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent 0b7c958 commit b119911

File tree

1 file changed

+31
-0
lines changed
  • mcp-server/app/api/cloud/fix-workflow

1 file changed

+31
-0
lines changed

mcp-server/app/api/cloud/fix-workflow/steps.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,37 @@ async function fetchAndUploadD3kArtifacts(
529529
console.log(`[D3k Artifacts] Could not fetch metadata`)
530530
}
531531
}
532+
533+
// 5. FALLBACK: Parse CLS score directly from logs if metadata is unavailable or missing totalCLS
534+
// This is a fallback because the metadata endpoint may not always be available
535+
if (!result.metadata || (result.metadata as { totalCLS?: number })?.totalCLS === undefined) {
536+
console.log(`[D3k Artifacts] Attempting to parse CLS from logs as fallback...`)
537+
// Parse layout shift scores from logs like:
538+
// [BROWSER] [CDP] Layout shift detected (element: DIV, position: static, score: 0.4716, time: 1025ms)
539+
const clsRegex = /Layout shift detected.*score:\s*([\d.]+)/g
540+
const clsMatches = [...result.fullLogs.matchAll(clsRegex)]
541+
if (clsMatches.length > 0) {
542+
const scores = clsMatches.map((m) => parseFloat(m[1]))
543+
const totalCLS = scores.reduce((sum, s) => sum + s, 0)
544+
const clsGrade: "good" | "needs-improvement" | "poor" =
545+
totalCLS <= 0.1 ? "good" : totalCLS <= 0.25 ? "needs-improvement" : "poor"
546+
547+
console.log(`[D3k Artifacts] Parsed ${scores.length} layout shifts from logs, total CLS: ${totalCLS}`)
548+
result.metadata = {
549+
...result.metadata,
550+
totalCLS,
551+
clsGrade,
552+
layoutShifts: scores.map((score, i) => ({
553+
score,
554+
timestamp: i * 100, // Approximate timestamps
555+
sources: []
556+
})),
557+
parsedFromLogs: true // Flag to indicate this was parsed from logs
558+
}
559+
} else {
560+
console.log(`[D3k Artifacts] No layout shift data found in logs`)
561+
}
562+
}
532563
}
533564

534565
console.log(

0 commit comments

Comments
 (0)