From 7c6f88ba6ea7d3eb0367f4790d379b6bd210a0f5 Mon Sep 17 00:00:00 2001 From: Jinjing <6427696+AmethystLiang@users.noreply.github.com> Date: Sun, 28 Jun 2026 16:53:32 -0700 Subject: [PATCH] fix: address review findings (#6666) --- .../github/check-job-log-tail-slice.test.ts | 53 ++++++++++++ src/main/github/check-job-log-tail-slice.ts | 84 +++++++++++++++++++ src/main/github/client.ts | 12 +-- .../src/components/editor/CheckRunJobs.tsx | 2 +- .../right-sidebar/check-job-log-tail.tsx | 57 +++++++++++-- .../checks-panel-content.test.tsx | 16 +++- src/renderer/src/i18n/locales/en.json | 4 +- 7 files changed, 207 insertions(+), 21 deletions(-) create mode 100644 src/main/github/check-job-log-tail-slice.test.ts create mode 100644 src/main/github/check-job-log-tail-slice.ts diff --git a/src/main/github/check-job-log-tail-slice.test.ts b/src/main/github/check-job-log-tail-slice.test.ts new file mode 100644 index 000000000..aad235f49 --- /dev/null +++ b/src/main/github/check-job-log-tail-slice.test.ts @@ -0,0 +1,53 @@ +import { describe, expect, it } from 'vitest' +import { + PR_CHECK_LOG_TAIL_BYTES, + PR_CHECK_LOG_TAIL_EARLIER_SEPARATOR, + sliceCheckLogTail +} from './check-job-log-tail-slice' + +describe('sliceCheckLogTail', () => { + it('keeps the recent tail when no earlier error markers are present', () => { + const logLines = Array.from({ length: 210 }, (_, index) => `line ${index}`) + const sliced = sliceCheckLogTail(logLines.join('\n')) + + expect(sliced).toContain('line 209') + expect(sliced).not.toContain('line 0') + expect(Buffer.from(sliced, 'utf8').byteLength).toBeLessThanOrEqual(PR_CHECK_LOG_TAIL_BYTES) + }) + + it('pulls earlier error lines into the excerpt when the recent tail is noisy', () => { + const noisyPrefix = Array.from({ length: 120 }, (_, index) => `Setting up package-${index}`) + const failure = '##[error]Process completed with exit code 236.' + const noisySuffix = Array.from({ length: 100 }, (_, index) => `Running trigger ${index}`) + const sliced = sliceCheckLogTail([...noisyPrefix, failure, ...noisySuffix].join('\n')) + + expect(sliced).toContain(failure) + expect(sliced).toContain(PR_CHECK_LOG_TAIL_EARLIER_SEPARATOR) + expect(sliced).toContain('Running trigger 39') + expect(sliced).not.toContain('Setting up package-0') + expect(Buffer.from(sliced, 'utf8').byteLength).toBeLessThanOrEqual(PR_CHECK_LOG_TAIL_BYTES) + }) + + it('still applies the byte cap after combining earlier errors with the recent tail', () => { + const logLines = Array.from({ length: 220 }, (_, index) => `line ${index} ${'x'.repeat(120)}`) + const sliced = sliceCheckLogTail(logLines.join('\n')) + + expect(sliced).toContain('line 219') + expect(Buffer.from(sliced, 'utf8').byteLength).toBeLessThanOrEqual(PR_CHECK_LOG_TAIL_BYTES) + }) + + it('keeps earlier error context when the recent tail is larger than the byte cap', () => { + const noisyPrefix = Array.from({ length: 120 }, (_, index) => `Installing package ${index}`) + const failure = 'AssertionError: expected visible failure' + const hugeRecentTail = Array.from( + { length: 100 }, + (_, index) => `recent line ${index} ${'x'.repeat(300)}` + ) + const sliced = sliceCheckLogTail([...noisyPrefix, failure, ...hugeRecentTail].join('\n')) + + expect(sliced).toContain(failure) + expect(sliced).toContain(PR_CHECK_LOG_TAIL_EARLIER_SEPARATOR) + expect(sliced).toContain('recent line 99') + expect(Buffer.from(sliced, 'utf8').byteLength).toBeLessThanOrEqual(PR_CHECK_LOG_TAIL_BYTES) + }) +}) diff --git a/src/main/github/check-job-log-tail-slice.ts b/src/main/github/check-job-log-tail-slice.ts new file mode 100644 index 000000000..694a5dfec --- /dev/null +++ b/src/main/github/check-job-log-tail-slice.ts @@ -0,0 +1,84 @@ +export const PR_CHECK_LOG_TAIL_LINES = 200 +export const PR_CHECK_LOG_TAIL_RECENT_LINES = 100 +export const PR_CHECK_LOG_TAIL_BYTES = 16 * 1024 +export const PR_CHECK_LOG_TAIL_ERROR_CONTEXT_LINES = 2 +export const PR_CHECK_LOG_TAIL_MAX_EARLIER_LINES = 30 +export const PR_CHECK_LOG_TAIL_EARLIER_SEPARATOR = '… earlier errors …' + +// Why: install/cache steps can flood the tail; pull a small window around earlier +// error markers so failures remain visible without downloading the full job log. +const ERROR_LINE_PATTERN = + /(?:##\[error\]|::error::|::error\b|\berror:|FAILED|exit code|ENOENT|EACCES|panic:|AssertionError)/i + +function applyLogTailByteCap(text: string): string { + if (Buffer.byteLength(text, 'utf8') <= PR_CHECK_LOG_TAIL_BYTES) { + return text + } + return sliceTrailingTextByUtf8Bytes(text, PR_CHECK_LOG_TAIL_BYTES) +} + +function sliceTrailingTextByUtf8Bytes(text: string, byteLimit: number): string { + let byteLength = 0 + const characters = Array.from(text) + for (let index = characters.length - 1; index >= 0; index -= 1) { + const characterByteLength = Buffer.byteLength(characters[index] ?? '', 'utf8') + if (byteLength + characterByteLength > byteLimit) { + return characters.slice(index + 1).join('') + } + byteLength += characterByteLength + } + return text +} + +function joinLogExcerptWithByteCap(prefixLines: string[], recentLines: string[]): string { + const prefix = prefixLines.join('\n') + const prefixByteLength = Buffer.byteLength(prefix, 'utf8') + if (prefixByteLength >= PR_CHECK_LOG_TAIL_BYTES) { + return sliceTrailingTextByUtf8Bytes(prefix, PR_CHECK_LOG_TAIL_BYTES) + } + + const separator = prefix.length > 0 && recentLines.length > 0 ? '\n' : '' + const recentBudget = + PR_CHECK_LOG_TAIL_BYTES - prefixByteLength - Buffer.byteLength(separator, 'utf8') + const recentTail = sliceTrailingTextByUtf8Bytes(recentLines.join('\n'), recentBudget) + return `${prefix}${separator}${recentTail}` +} + +function collectEarlierErrorLineIndexes(lines: string[], recentStart: number): number[] { + const indexes = new Set() + for (let index = 0; index < recentStart; index += 1) { + if (!ERROR_LINE_PATTERN.test(lines[index] ?? '')) { + continue + } + const contextStart = Math.max(0, index - PR_CHECK_LOG_TAIL_ERROR_CONTEXT_LINES) + const contextEnd = Math.min(recentStart - 1, index + PR_CHECK_LOG_TAIL_ERROR_CONTEXT_LINES) + for (let contextIndex = contextStart; contextIndex <= contextEnd; contextIndex += 1) { + indexes.add(contextIndex) + } + } + return [...indexes].sort((left, right) => left - right) +} + +export function sliceCheckLogTail(logText: string): string { + const lines = logText.split(/\r?\n/) + const recentStart = Math.max(0, lines.length - PR_CHECK_LOG_TAIL_RECENT_LINES) + const recentLines = lines.slice(recentStart) + + if (recentStart === 0) { + return applyLogTailByteCap(lines.join('\n')) + } + + const earlierIndexes = collectEarlierErrorLineIndexes(lines, recentStart) + if (earlierIndexes.length === 0) { + return applyLogTailByteCap(lines.slice(-PR_CHECK_LOG_TAIL_LINES).join('\n')) + } + + const cappedEarlierIndexes = earlierIndexes.slice(-PR_CHECK_LOG_TAIL_MAX_EARLIER_LINES) + const earlierLines = cappedEarlierIndexes.map((index) => lines[index] ?? '') + // Why: if the recent tail alone exceeds the byte cap, keep the earlier error + // context visible instead of truncating it back out of the combined excerpt. + return joinLogExcerptWithByteCap( + [...earlierLines, PR_CHECK_LOG_TAIL_EARLIER_SEPARATOR], + recentLines + ) +} diff --git a/src/main/github/client.ts b/src/main/github/client.ts index 99843f88b..3b2e605a5 100644 --- a/src/main/github/client.ts +++ b/src/main/github/client.ts @@ -37,6 +37,7 @@ import { import { mkdtemp, readFile, rm, writeFile } from 'fs/promises' import { join } from 'path' import { tmpdir } from 'os' +import { sliceCheckLogTail } from './check-job-log-tail-slice' import { getPRConflictSummary } from './conflict-summary' import { getSshFilesystemProvider } from '../providers/ssh-filesystem-dispatch' import { joinWorktreeRelativePath } from '../runtime/runtime-relative-paths' @@ -98,8 +99,6 @@ type GhExecOptions = ReturnType type HostedReviewLocalGitOptions = ReturnType const ORCA_REPO = 'stablyai/orca' -const PR_CHECK_LOG_TAIL_LINES = 200 -const PR_CHECK_LOG_TAIL_BYTES = 16 * 1024 const PR_CHECK_LOG_TAIL_JOB_LIMIT = 5 // Why: each entry holds up to 16KB of log text; bound the cache so a long // session reviewing many failing checks can't grow it without limit. @@ -3155,15 +3154,6 @@ function isCheckJobFailureState(state: string | null | undefined): boolean { ) } -function sliceCheckLogTail(logText: string): string { - const lineTail = logText.split(/\r?\n/).slice(-PR_CHECK_LOG_TAIL_LINES).join('\n') - const bytes = Buffer.from(lineTail, 'utf8') - if (bytes.byteLength <= PR_CHECK_LOG_TAIL_BYTES) { - return lineTail - } - return bytes.subarray(bytes.byteLength - PR_CHECK_LOG_TAIL_BYTES).toString('utf8') -} - function getCheckJobLogTailCacheKey(job: PRCheckRunDetails['jobs'][number]): string | null { if (job.id === null) { return null diff --git a/src/renderer/src/components/editor/CheckRunJobs.tsx b/src/renderer/src/components/editor/CheckRunJobs.tsx index 4818aafcd..6b3bfa081 100644 --- a/src/renderer/src/components/editor/CheckRunJobs.tsx +++ b/src/renderer/src/components/editor/CheckRunJobs.tsx @@ -137,7 +137,7 @@ function JobCard({ job, index }: { job: PRCheckJob; index: number }): React.JSX. )} - {job.logTail && } + {job.logTail && } ) } diff --git a/src/renderer/src/components/right-sidebar/check-job-log-tail.tsx b/src/renderer/src/components/right-sidebar/check-job-log-tail.tsx index 8f13a4f16..e5f3d98cc 100644 --- a/src/renderer/src/components/right-sidebar/check-job-log-tail.tsx +++ b/src/renderer/src/components/right-sidebar/check-job-log-tail.tsx @@ -1,6 +1,29 @@ -import React, { useCallback, useRef, useState } from 'react' +import React, { useCallback, useEffect, useRef, useState } from 'react' import { Check, Copy } from 'lucide-react' import { translate } from '@/i18n/i18n' +import { cn } from '@/lib/utils' + +const LOG_EXCERPT_ERROR_LINE_PATTERN = + /(?:##\[error\]|::error::|::error\b|\berror:|FAILED|exit code|ENOENT|EACCES|panic:|AssertionError)/i + +function getLogExcerptScrollTop(pre: HTMLPreElement, logTail: string): number { + const lines = logTail.split(/\r?\n/) + let targetLineIndex = lines.length - 1 + for (let index = 0; index < lines.length; index += 1) { + if (LOG_EXCERPT_ERROR_LINE_PATTERN.test(lines[index] ?? '')) { + targetLineIndex = index + } + } + + const lineHeight = Number.parseFloat(getComputedStyle(pre).lineHeight) + const approximateLineHeight = Number.isFinite(lineHeight) ? lineHeight : 16 + const targetScroll = targetLineIndex * approximateLineHeight + const maxScroll = Math.max(0, pre.scrollHeight - pre.clientHeight) + if (targetLineIndex < lines.length - 1) { + return Math.min(maxScroll, Math.max(0, targetScroll - pre.clientHeight / 3)) + } + return maxScroll +} function CopyButton({ text, @@ -62,25 +85,49 @@ function CopyButton({ ) } -export function CheckJobLogTail({ logTail }: { logTail: string }): React.JSX.Element { +export function CheckJobLogTail({ + logTail, + expanded = false +}: { + logTail: string + expanded?: boolean +}): React.JSX.Element { + const logPreRef = useRef(null) + + useEffect(() => { + const logPre = logPreRef.current + if (!logPre) { + return + } + // Why: noisy install/cache output buries failures at the top of the excerpt; + // jump to the last error marker when present, otherwise the log end. + logPre.scrollTop = getLogExcerptScrollTop(logPre, logTail) + }, [expanded, logTail]) + return (
{translate( 'auto.components.right.sidebar.checks.panel.content.d713f500b2', - 'Log tail (last 200 lines)' + 'Log excerpt' )}
-
+      
         {logTail}
       
diff --git a/src/renderer/src/components/right-sidebar/checks-panel-content.test.tsx b/src/renderer/src/components/right-sidebar/checks-panel-content.test.tsx index c3fa3ffd2..6ce6dc4f8 100644 --- a/src/renderer/src/components/right-sidebar/checks-panel-content.test.tsx +++ b/src/renderer/src/components/right-sidebar/checks-panel-content.test.tsx @@ -377,8 +377,20 @@ describe('CheckJobLogTail', () => { }) ) - expect(markup).toContain('Log tail (last 200 lines)') + expect(markup).toContain('Log excerpt') expect(markup).toContain('Error: expected true to be false') - expect(markup).toContain('Copy log tail') + expect(markup).toContain('Copy log excerpt') + }) + + it('uses a taller viewport when expanded for failed-job details', () => { + const markup = renderToStaticMarkup( + React.createElement(CheckJobLogTail, { + logTail: 'Error: expected true to be false', + expanded: true + }) + ) + + expect(markup).toContain('min-h-48') + expect(markup).toContain('max-h-[min(50vh,32rem)]') }) }) diff --git a/src/renderer/src/i18n/locales/en.json b/src/renderer/src/i18n/locales/en.json index 0c03e01db..559023da3 100644 --- a/src/renderer/src/i18n/locales/en.json +++ b/src/renderer/src/i18n/locales/en.json @@ -8996,8 +8996,8 @@ "5e52f4ef7f": "failing", "02ca4f9074": "passing", "e15a8b77ef": "No inline details are available for this check.", - "679bf2093c": "Copy log tail", - "d713f500b2": "Log tail (last 200 lines)", + "679bf2093c": "Copy log excerpt", + "d713f500b2": "Log excerpt", "a916648574": "Open details", "07eccfa397": "No details are available for this check.", "49731703ea": "Jobs",