fix: address review findings (#6666)
This commit is contained in:
@@ -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)
|
||||
})
|
||||
})
|
||||
@@ -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<number>()
|
||||
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
|
||||
)
|
||||
}
|
||||
@@ -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<typeof ghRepoExecOptions>
|
||||
type HostedReviewLocalGitOptions = ReturnType<typeof getHostedReviewLocalGitOptions>
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user