Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 | 28x 72x 50x 50x 50x 50x 2967x 2967x 7x 7x 2960x 7x 7x 2953x 462x 462x 2491x 717x 717x 81x 81x 47x 3x 32x 32x 32x 3x 29x 29x 29x 1x 28x 28x 1x 27x 27x 27x 1x 26x 28x 28x 28x 6x 3x 3x 6x 1x 2x 2x 2x 2x 2x 2x 2x 6x 7x 7x 7x 7x 5x 2x 1x 1x 1x 1x 1x 1x 28x 28x 209x 209x 9x 9x 9x 7x 7x 7x 9x 9x 8x 9x 7x 7x 5x 5x 5x 1x 1x 4x 4x 26x 12x 12x 11x 11x 10x 12x 11x 11x 11x 1x 10x 10x 10x 10x 10x 14x 14x 12x 12x 10x 9x 9x 9x 9x 9x 9x 19x 19x 9x 8x 8x 8x 8x 17x 17x 17x 17x 8x 8x 8x 8x 17x 9x 9x 1x 1x 1x 1x 28x 22x 22x 22x 22x 22x 22x 22x 19x 19x 28x 19x 9x 10x 22x 22x 10x 10x 10x 1x 9x 9x 1x 8x 9x 8x 19x 10x 8x | import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { z } from 'zod';
import { customAlphabet } from 'nanoid';
import { FindingSchema, compareFindingPriority } from '../types/index.js';
import type { Finding, Location, UsageStats } from '../types/index.js';
import { getRuntime } from './runtimes/index.js';
import type { RuntimeName } from './runtimes/index.js';
import type { FindingProcessingEvent } from './types.js';
import {
buildJsonOutputSection,
buildTaggedSection,
formatIndexedFindingsForPrompt,
joinPromptSections,
} from './prompt-sections.js';
/** Pattern to match the start of findings JSON (allows whitespace after brace) */
export const FINDINGS_JSON_START = /\{\s*"findings"/;
/**
* Result from extracting findings JSON from text.
*/
export type ExtractFindingsResult =
| { success: true; findings: unknown[]; usage?: UsageStats }
| { success: false; error: string; preview: string; usage?: UsageStats };
export interface AuxiliaryCallOptions {
apiKey?: string;
runtime?: RuntimeName;
model?: string;
maxRetries?: number;
agentName?: string;
}
/** Return true when the selected runtime can authenticate outside a legacy Anthropic API key. */
export function canUseRuntimeAuth(options?: Pick<AuxiliaryCallOptions, 'apiKey' | 'runtime'>): boolean {
// A missing runtime means a direct helper call, not the configured pipeline default.
return Boolean(options?.apiKey) || (options?.runtime ?? 'claude') !== 'claude';
}
/**
* Extract JSON object from text, handling nested braces correctly.
* Starts from the given position and returns the balanced JSON object.
*/
export function extractBalancedJson(text: string, startIndex: number): string | null {
let depth = 0;
let inString = false;
let escape = false;
for (let i = startIndex; i < text.length; i++) {
const char = text[i];
if (escape) {
escape = false;
continue;
}
if (char === '\\' && inString) {
escape = true;
continue;
}
if (char === '"') {
inString = !inString;
continue;
}
if (inString) continue;
if (char === '{') depth++;
if (char === '}') {
depth--;
if (depth === 0) {
return text.slice(startIndex, i + 1);
}
}
}
return null;
}
/**
* Extract findings JSON from model output text.
* Handles markdown code fences, prose before JSON, and nested objects.
*/
export function extractFindingsJson(rawText: string): ExtractFindingsResult {
const text = rawText.trim();
// Find the start of the findings JSON object
const findingsMatch = text.match(FINDINGS_JSON_START);
if (!findingsMatch || findingsMatch.index === undefined) {
return {
success: false,
error: 'no_findings_json',
preview: text.slice(0, 200),
};
}
const findingsStart = findingsMatch.index;
// Extract the balanced JSON object
const jsonStr = extractBalancedJson(text, findingsStart);
if (!jsonStr) {
return {
success: false,
error: 'unbalanced_json',
preview: text.slice(findingsStart, findingsStart + 200),
};
}
// Parse the JSON
let parsed: unknown;
try {
parsed = JSON.parse(jsonStr);
} catch {
return {
success: false,
error: 'invalid_json',
preview: jsonStr.slice(0, 200),
};
}
// Validate structure
Iif (typeof parsed !== 'object' || parsed === null || !('findings' in parsed)) {
return {
success: false,
error: 'missing_findings_key',
preview: jsonStr.slice(0, 200),
};
}
const findings = (parsed as { findings: unknown }).findings;
if (!Array.isArray(findings)) {
return {
success: false,
error: 'findings_not_array',
preview: jsonStr.slice(0, 200),
};
}
return { success: true, findings };
}
/** Max characters to send to LLM fallback (roughly ~8k tokens) */
const LLM_FALLBACK_MAX_CHARS = 32000;
/** Max tokens for LLM fallback responses */
const LLM_FALLBACK_MAX_TOKENS = 4096;
/** Timeout for LLM fallback API calls in milliseconds */
const LLM_FALLBACK_TIMEOUT_MS = 30000;
/**
* Truncate text for LLM fallback while preserving the findings JSON.
*
* Caller must ensure findings JSON exists in the text before calling.
*/
export function truncateForLLMFallback(rawText: string, maxChars: number): string {
if (rawText.length <= maxChars) {
return rawText;
}
const findingsIndex = rawText.match(FINDINGS_JSON_START)?.index ?? -1;
// If findings starts within our budget, simple truncation from start preserves it
if (findingsIndex < maxChars - 20) {
return rawText.slice(0, maxChars) + '\n[... truncated]';
}
// Findings is beyond our budget - skip to just before it
// Keep minimal context (10% of budget or 200 chars, whichever is smaller)
const markerOverhead = 40;
const usableBudget = maxChars - markerOverhead;
const contextBefore = Math.min(200, Math.floor(usableBudget * 0.1), findingsIndex);
const startIndex = findingsIndex - contextBefore;
const endIndex = startIndex + usableBudget;
const truncatedContent = rawText.slice(startIndex, endIndex);
const suffix = endIndex < rawText.length ? '\n[... truncated]' : '';
return '[... truncated ...]\n' + truncatedContent + suffix;
}
/**
* Extract findings from malformed output using LLM as a fallback.
* Uses the configured auxiliary runtime for lightweight, structured extraction.
*/
export async function extractFindingsWithLLM(
rawText: string,
apiKeyOrOptions?: string | AuxiliaryCallOptions,
maxRetries?: number
): Promise<ExtractFindingsResult> {
const options: AuxiliaryCallOptions =
typeof apiKeyOrOptions === 'object'
? apiKeyOrOptions
: { apiKey: apiKeyOrOptions, maxRetries };
const { apiKey, runtime, model } = options;
const runtimeName = runtime ?? 'claude';
if (!canUseRuntimeAuth(options)) {
return {
success: false,
error: 'no_api_key_for_fallback',
preview: rawText.slice(0, 200),
};
}
// If no findings anchor exists, there's nothing to extract
if (!FINDINGS_JSON_START.test(rawText)) {
return {
success: false,
error: 'no_findings_to_extract',
preview: rawText.slice(0, 200),
};
}
// Truncate input while preserving JSON boundaries
const truncatedText = truncateForLLMFallback(rawText, LLM_FALLBACK_MAX_CHARS);
const userContent = joinPromptSections([
`<task>
Extract the findings JSON from this model output.
</task>`,
buildJsonOutputSection(`Return this shape: {"findings": [...]}
If no findings exist, return: {"findings": []}`),
buildTaggedSection('model_output', truncatedText),
]);
const result = await getRuntime(runtimeName).runAuxiliary({
task: 'extraction',
agentName: options.agentName,
apiKey,
prompt: userContent,
schema: z.object({ findings: z.array(z.unknown()) }),
model,
maxTokens: LLM_FALLBACK_MAX_TOKENS,
timeout: LLM_FALLBACK_TIMEOUT_MS,
maxRetries: options.maxRetries,
});
Eif (!result.success) {
return {
success: false,
error: `llm_extraction_failed: ${result.error}`,
preview: rawText.slice(0, 200),
usage: result.usage,
};
}
return {
success: true,
findings: result.data.findings,
usage: result.usage,
};
}
/** Unambiguous uppercase alphanumeric alphabet (no O/0, I/1). */
const SHORT_ID_ALPHABET = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
/** Length of each generated short ID (before formatting). */
export const SHORT_ID_LENGTH = 6;
/**
* Generate a short human-readable ID for a finding.
* Format: XXX-XXX (e.g., K7M-X9P)
*/
export function generateShortId(): string {
const raw = customAlphabet(SHORT_ID_ALPHABET, SHORT_ID_LENGTH)();
return `${raw.slice(0, 3)}-${raw.slice(3)}`;
}
/**
* Validate and normalize findings from extracted JSON.
* Replaces the LLM-provided ID with a short nanoid for stable cross-referencing.
*/
export function validateFindings(findings: unknown[], filename: string): Finding[] {
const validated: Finding[] = [];
for (const f of findings) {
// Normalize location path before validation
if (typeof f === 'object' && f !== null && 'location' in f) {
const loc = (f as Record<string, unknown>)['location'];
Eif (loc && typeof loc === 'object') {
(loc as Record<string, unknown>)['path'] = filename;
}
}
const result = FindingSchema.safeParse(f);
if (result.success) {
validated.push({
...result.data,
id: generateShortId(),
location: result.data.location ? { ...result.data.location, path: filename } : undefined,
});
}
}
return validated;
}
type FindingProcessingCallback = (event: FindingProcessingEvent) => void;
/**
* Deduplicate findings by title and location.
*/
export function deduplicateFindings(
findings: Finding[],
onFindingProcessing?: FindingProcessingCallback
): Finding[] {
const seen = new Map<string, Finding>();
return findings.filter((f) => {
const key = `${f.title}:${f.location?.path}:${f.location?.startLine}`;
const kept = seen.get(key);
if (kept) {
onFindingProcessing?.({
stage: 'dedupe',
action: 'dropped',
finding: f,
replacement: kept,
reason: 'duplicate title and location',
});
return false;
}
seen.set(key, f);
return true;
});
}
// ---------------------------------------------------------------------------
// Cross-location merging
// ---------------------------------------------------------------------------
function locationKey(loc: Location): string {
return `${loc.path}:${loc.startLine}:${loc.endLine ?? ''}`;
}
/**
* Merge locations from loser findings into the winner.
* Each loser's primary location and any existing additionalLocations are
* appended to winner.additionalLocations (deduplicated).
*
* @param sortedGroup - Findings sorted by priority (winner first, losers after).
* @returns A shallow copy of the winner with merged locations, or undefined if empty.
*/
export function mergeGroupLocations(sortedGroup: Finding[]): Finding | undefined {
const winner = sortedGroup[0];
if (!winner) return undefined;
const losers = sortedGroup.slice(1);
if (losers.length === 0) return winner;
const extraLocations: Location[] = winner.additionalLocations
? [...winner.additionalLocations]
: [];
for (const loser of losers) {
Eif (loser.location) {
extraLocations.push(loser.location);
}
if (loser.additionalLocations) {
extraLocations.push(...loser.additionalLocations);
}
}
Iif (extraLocations.length === 0) return winner;
// Deduplicate by path:startLine:endLine, seeding with winner's primary location
const seen = new Set<string>();
Eif (winner.location) {
seen.add(locationKey(winner.location));
}
const uniqueLocations = extraLocations.filter((loc) => {
const key = locationKey(loc);
if (seen.has(key)) return false;
seen.add(key);
return true;
});
return { ...winner, additionalLocations: uniqueLocations };
}
/**
* Result of applying merge groups to a list of findings.
*/
interface ApplyGroupsResult {
/** Original findings that were absorbed into another finding's additionalLocations */
absorbed: Set<Finding>;
/** Map from original winner finding to its merged replacement (with additionalLocations) */
replacements: Map<Finding, Finding>;
}
/**
* Apply LLM-returned merge groups to a list of findings.
*
* For each group, the highest-priority finding becomes the winner, and all
* other findings' locations are folded into its additionalLocations.
* Handles overlapping groups by substituting prior replacements and tracking
* absorbed findings by their original identity.
*
* @param indexedFindings - The findings referenced by the 1-based group indices.
* @param groups - Arrays of 1-based indices grouping findings by shared root cause.
*/
export function applyMergeGroups(
indexedFindings: Finding[],
groups: number[][]
): ApplyGroupsResult {
const absorbed = new Set<Finding>();
const replacements = new Map<Finding, Finding>();
for (const group of groups) {
const uniqueIndices = [...new Set(group)];
Iif (uniqueIndices.length < 2) continue;
const groupFindings = uniqueIndices
.map((idx) => indexedFindings[idx - 1])
.filter((f): f is Finding => f !== undefined && !absorbed.has(f));
if (groupFindings.length < 2) continue;
// Sort to determine winner, then substitute any prior replacements
// so that locations accumulated from earlier groups carry forward.
const sorted = [...groupFindings].sort(compareFindingPriority);
const winner = sorted[0];
Iif (!winner) continue;
for (let i = 0; i < sorted.length; i++) {
const f = sorted[i];
Iif (!f) continue;
const existing = replacements.get(f);
Iif (existing) sorted[i] = existing;
}
const merged = mergeGroupLocations(sorted);
Eif (merged) {
replacements.set(winner, merged);
}
for (const f of groupFindings) {
if (f !== winner) {
absorbed.add(f);
}
}
}
return { absorbed, replacements };
}
function sameLocation(a: Location | undefined, b: Location | undefined): boolean {
return Boolean(a && b && locationKey(a) === locationKey(b));
}
function findReplacementForAbsorbed(
finding: Finding,
replacements: Map<Finding, Finding>
): Finding | undefined {
for (const replacement of replacements.values()) {
Eif (replacement.additionalLocations?.some((loc) => sameLocation(loc, finding.location))) {
return replacement;
}
}
return undefined;
}
/** Schema for LLM merge response: groups of finding indices sharing a root cause. */
const MergeGroupsSchema = z.array(z.array(z.number().int()));
/**
* Result from merging cross-location findings.
*/
export interface MergeResult {
findings: Finding[];
mergedCount: number;
usage?: UsageStats;
}
/**
* Read a code snippet from disk around a given line.
* Returns empty string on any I/O error.
*/
function readSnippet(repoPath: string, filePath: string, startLine: number, contextLines = 3): string {
try {
const fullPath = join(repoPath, filePath);
const content = readFileSync(fullPath, 'utf-8');
const lines = content.split('\n');
const start = Math.max(0, startLine - 1 - contextLines);
const end = Math.min(lines.length, startLine - 1 + contextLines + 1);
return lines.slice(start, end).join('\n');
} catch {
return '';
}
}
/**
* Merge findings that describe the same issue across different code locations.
*
* Uses the configured auxiliary runtime to identify groups of findings about
* the same root cause at different locations. For each group, the
* highest-priority finding becomes the primary; other locations move to
* `additionalLocations`.
*
* Skips entirely (no LLM call) when:
* - Fewer than 2 findings have locations
* - Claude runtime is selected and no API key is provided
*/
export async function mergeCrossLocationFindings(
findings: Finding[],
options?: AuxiliaryCallOptions & {
repoPath?: string;
onFindingProcessing?: FindingProcessingCallback;
}
): Promise<MergeResult> {
const apiKey = options?.apiKey;
const repoPath = options?.repoPath ?? '.';
// Early exit: need at least 2 located findings to merge
const withLocations = findings.filter((f) => f.location);
if (withLocations.length < 2 || !canUseRuntimeAuth(options)) {
return { findings, mergedCount: 0 };
}
const findingDescriptions = formatIndexedFindingsForPrompt(withLocations, {
locationStyle: 'range',
snippet: (finding) => {
const loc = finding.location;
return loc ? readSnippet(repoPath, loc.path, loc.startLine) : undefined;
},
});
const prompt = joinPromptSections([
`<task>
Identify which of these code review findings describe the SAME underlying issue appearing at different locations. Group them by shared root cause.
</task>`,
`<findings>
${findingDescriptions}
</findings>`,
buildJsonOutputSection(`Return a JSON array of arrays, where each inner array contains the 1-based indices of findings about the same issue.
Singletons should not appear. Return [] if no findings describe the same issue.`),
]);
const result = await getRuntime(options?.runtime ?? 'claude').runSynthesis({
task: 'consolidation',
agentName: options?.agentName,
apiKey,
prompt,
schema: MergeGroupsSchema,
model: options?.model,
maxTokens: 512,
maxRetries: options?.maxRetries,
});
if (!result.success) {
return { findings, mergedCount: 0, usage: result.usage };
}
const { absorbed, replacements } = applyMergeGroups(withLocations, result.data);
if (absorbed.size === 0) {
return { findings, mergedCount: 0, usage: result.usage };
}
for (const finding of absorbed) {
options?.onFindingProcessing?.({
stage: 'merge',
action: 'merged',
finding,
replacement: findReplacementForAbsorbed(finding, replacements),
reason: 'same root cause at another location',
});
}
const merged = findings
.filter((f) => !absorbed.has(f))
.map((f) => replacements.get(f) ?? f);
return { findings: merged, mergedCount: absorbed.size, usage: result.usage };
}
|