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 | 15x 88x 88x 180x 180x 599x 1x 1x 20x 19x 1x 19x 20x 1x 19x 19x 19x 19x 19x 19x 20x 20x 20x 20x 19x 19x 19x 19x 2x 2x 2x 17x 3x 3x 3x 1x 3x 1x 3x 2x 3x 3x 3x 3x 3x 14x 14x 5x 14x 3x 14x 6x 14x 14x 1x 14x 14x 11x 11x 11x 21x 21x 21x 4x 1x 3x 3x 13x 13x 10x 10x 6x 2x 4x 35x 35x 35x 444x 3x 441x 3x 438x 48x 48x 48x 166x 2x 164x | import chalk from 'chalk';
import figures from 'figures';
import type { SkillReport, Finding, FileChange, UsageStats, AuxiliaryUsageMap } from '../../types/index.js';
import { Verbosity } from './verbosity.js';
import { type OutputMode, timestamp } from './tty.js';
import {
formatDuration,
formatFindingCounts,
formatFindingCountsPlain,
formatUsage,
formatUsagePlain,
countBySeverity,
pluralize,
} from './formatters.js';
import { BoxRenderer } from './box.js';
import { ICON_CHECK } from './icons.js';
import { getVersion } from '../../utils/index.js';
import { mergeAuxiliaryUsage } from '../../sdk/usage.js';
/**
* Map a file change status to its single-character symbol.
*/
function statusSymbol(status: string): string {
if (status === 'added') return '+';
if (status === 'removed') return '-';
return '~';
}
/**
* Map a file change status to a colored symbol for TTY output.
*/
function coloredStatusSymbol(status: string): string {
const sym = statusSymbol(status);
if (status === 'added') return chalk.green(sym);
if (status === 'removed') return chalk.red(sym);
return chalk.yellow(sym);
}
/**
* ASCII art logo for TTY header.
*/
const LOGO = `
__ __ _
/ / /\\ \\ \\__ _ _ __ __| | ___ _ __
\\ \\/ \\/ / _\` | '__/ _\` |/ _ \\ '_ \\
\\ /\\ / (_| | | | (_| | __/ | | |
\\/ \\/ \\__,_|_| \\__,_|\\___|_| |_|
`.replace(/^\n/, '');
/**
* Callbacks for skill runner progress reporting.
*/
export interface SkillRunnerCallbacks {
/** Start time of the skill execution (for elapsed time calculations) */
skillStartTime?: number;
onFileStart?: (file: string, index: number, total: number) => void;
onHunkStart?: (file: string, hunkNum: number, total: number, lineRange: string) => void;
onHunkComplete?: (file: string, hunkNum: number, findings: Finding[], usage: UsageStats) => void;
onFileComplete?: (file: string, index: number, total: number) => void;
}
/**
* Main reporter class for CLI output.
* Handles different verbosity levels and TTY/non-TTY modes.
*
* Reporter spec: specs/reporters.md
*/
export class Reporter {
readonly mode: OutputMode;
readonly verbosity: Verbosity;
constructor(mode: OutputMode, verbosity: Verbosity) {
this.mode = mode;
this.verbosity = verbosity;
}
/**
* Output to stderr (status messages).
*/
private log(message: string): void {
Iif (this.verbosity === Verbosity.Quiet) {
return;
}
console.error(message);
}
/**
* Output to stderr with timestamp (plain/log mode).
*/
private logPlain(message: string): void {
console.error(`[${timestamp()}] warden: ${message}`);
}
/**
* Print the header with logo and version.
*/
header(): void {
Eif (this.verbosity === Verbosity.Quiet) {
return;
}
if (this.mode.isTTY) {
this.log('');
for (const line of LOGO.split('\n')) {
this.log(chalk.dim(line));
}
this.log(chalk.dim(`v${getVersion()}`));
this.log('');
} else {
this.logPlain(`Warden v${getVersion()}`);
}
}
/**
* Start the context section (e.g., "Analyzing changes from HEAD~3...")
*/
startContext(description: string): void {
if (this.verbosity === Verbosity.Quiet) {
return;
}
if (this.mode.isTTY) {
this.log(chalk.dim(description));
this.log('');
} else {
this.logPlain(description);
}
}
/**
* Display the list of files being analyzed.
*/
contextFiles(files: FileChange[]): void {
if (this.verbosity === Verbosity.Quiet) {
return;
}
const totalChunks = files.reduce((sum, f) => sum + (f.chunks ?? 0), 0);
const displayFiles = files.slice(0, 10);
if (this.mode.isTTY) {
this.log(
chalk.bold('FILES') +
chalk.cyan(` ${files.length} files`) +
chalk.dim(` · ${totalChunks} chunks`)
);
for (const file of displayFiles) {
const chunkInfo = file.chunks ? chalk.dim(` (${file.chunks} ${pluralize(file.chunks, 'chunk')})`) : '';
this.log(` ${coloredStatusSymbol(file.status)} ${file.filename}${chunkInfo}`);
}
if (files.length > 10) {
this.log(chalk.dim(` ... and ${files.length - 10} more`));
}
this.log('');
} else {
this.logPlain(`Found ${files.length} changed files with ${totalChunks} chunks`);
for (const file of displayFiles) {
const chunkInfo = file.chunks ? ` (${file.chunks} ${pluralize(file.chunks, 'chunk')})` : '';
this.logPlain(` ${statusSymbol(file.status)} ${file.filename}${chunkInfo}`);
}
if (files.length > 10) {
this.logPlain(` ... and ${files.length - 10} more`);
}
}
}
/**
* Aggregate usage stats from multiple reports.
*/
private aggregateUsage(reports: SkillReport[]): UsageStats | undefined {
const usages = reports.map((r) => r.usage).filter((u): u is UsageStats => u !== undefined);
if (usages.length === 0) return undefined;
return usages.reduce((acc, u) => ({
inputTokens: acc.inputTokens + u.inputTokens,
outputTokens: acc.outputTokens + u.outputTokens,
cacheReadInputTokens: (acc.cacheReadInputTokens ?? 0) + (u.cacheReadInputTokens ?? 0),
cacheCreationInputTokens: (acc.cacheCreationInputTokens ?? 0) + (u.cacheCreationInputTokens ?? 0),
cacheCreation5mInputTokens: (acc.cacheCreation5mInputTokens ?? 0) + (u.cacheCreation5mInputTokens ?? 0),
cacheCreation1hInputTokens: (acc.cacheCreation1hInputTokens ?? 0) + (u.cacheCreation1hInputTokens ?? 0),
webSearchRequests: (acc.webSearchRequests ?? 0) + (u.webSearchRequests ?? 0),
costUSD: acc.costUSD + u.costUSD,
}));
}
/**
* Aggregate auxiliary usage stats from multiple reports.
*/
private aggregateAuxiliaryUsage(reports: SkillReport[]): AuxiliaryUsageMap | undefined {
let totalAuxiliaryUsage: AuxiliaryUsageMap | undefined;
for (const report of reports) {
if (report.auxiliaryUsage) {
totalAuxiliaryUsage = mergeAuxiliaryUsage(totalAuxiliaryUsage, report.auxiliaryUsage);
}
}
return totalAuxiliaryUsage;
}
/**
* Render the summary section.
*/
renderSummary(reports: SkillReport[], totalDuration: number, options?: { traceId?: string }): void {
const allFindings: Finding[] = [];
let totalFailedHunks = 0;
let totalFailedExtractions = 0;
let totalSkippedFiles = 0;
for (const report of reports) {
allFindings.push(...report.findings);
totalFailedHunks += report.failedHunks ?? 0;
totalFailedExtractions += report.failedExtractions ?? 0;
totalSkippedFiles += report.skippedFiles?.length ?? 0;
}
const counts = countBySeverity(allFindings);
const totalUsage = this.aggregateUsage(reports);
const totalAuxiliaryUsage = this.aggregateAuxiliaryUsage(reports);
if (this.verbosity === Verbosity.Quiet) {
// Quiet mode: just output the summary line
const countStr = formatFindingCountsPlain(counts);
console.log(countStr);
return;
}
if (this.mode.isTTY) {
this.log(chalk.bold('SUMMARY'));
this.log(formatFindingCounts(counts));
if (totalFailedHunks > 0) {
this.log(chalk.yellow(`${figures.warning} ${totalFailedHunks} ${pluralize(totalFailedHunks, 'chunk')} failed to analyze`));
}
if (totalFailedExtractions > 0) {
this.log(chalk.yellow(`${figures.warning} ${totalFailedExtractions} finding ${pluralize(totalFailedExtractions, 'extraction')} failed`));
}
if ((totalFailedHunks > 0 || totalFailedExtractions > 0) && this.verbosity < Verbosity.Verbose) {
this.log(chalk.dim(' Use -v for failure details'));
}
Iif (totalSkippedFiles > 0) {
this.log(chalk.dim(`${totalSkippedFiles} ${pluralize(totalSkippedFiles, 'file')} skipped`));
}
const durationLine = `Analysis completed in ${formatDuration(totalDuration)}`;
Iif (totalUsage) {
this.log(chalk.dim(`${durationLine} · ${formatUsage(totalUsage, totalAuxiliaryUsage)}`));
} else {
this.log(chalk.dim(durationLine));
}
Iif (options?.traceId && this.verbosity >= Verbosity.Verbose) {
this.log(chalk.dim(`Trace: ${options.traceId}`));
}
} else {
this.logPlain(`Summary: ${formatFindingCountsPlain(counts)}`);
if (totalFailedHunks > 0) {
this.logPlain(`WARN: ${totalFailedHunks} ${pluralize(totalFailedHunks, 'chunk')} failed to analyze`);
}
if (totalFailedExtractions > 0) {
this.logPlain(`WARN: ${totalFailedExtractions} finding ${pluralize(totalFailedExtractions, 'extraction')} failed`);
}
if ((totalFailedHunks > 0 || totalFailedExtractions > 0) && this.verbosity < Verbosity.Verbose) {
this.logPlain('Use -v for failure details');
}
Iif (totalSkippedFiles > 0) {
this.logPlain(`${totalSkippedFiles} ${pluralize(totalSkippedFiles, 'file')} skipped`);
}
if (totalUsage) {
this.logPlain(`Usage: ${formatUsagePlain(totalUsage, totalAuxiliaryUsage)}`);
}
this.logPlain(`Total time: ${formatDuration(totalDuration)}`);
Iif (options?.traceId && this.verbosity >= Verbosity.Verbose) {
this.logPlain(`Trace: ${options.traceId}`);
}
}
}
/**
* Display the configuration section with triggers.
*/
configTriggers(
loaded: number,
matched: number,
triggers: { name: string; skill: string }[]
): void {
if (this.verbosity === Verbosity.Quiet) {
return;
}
if (this.mode.isTTY) {
this.log(
chalk.bold('CONFIG') +
chalk.cyan(` ${loaded} triggers`) +
chalk.dim(` · ${matched} matched`)
);
// Show matched triggers
for (const trigger of triggers) {
this.log(` ${chalk.green(ICON_CHECK)} ${trigger.name} ${chalk.dim(`(${trigger.skill})`)}`);
}
this.log('');
} else {
this.logPlain(`Config: ${loaded} triggers, ${matched} matched`);
for (const trigger of triggers) {
this.logPlain(` ${trigger.name} (${trigger.skill})`);
}
}
}
/**
* Log a step message.
*/
step(message: string): void {
Iif (this.verbosity === Verbosity.Quiet) {
return;
}
Iif (this.mode.isTTY) {
this.log(`${chalk.cyan(figures.arrowRight)} ${message}`);
} else {
this.logPlain(message);
}
}
/**
* Log a success message.
*/
success(message: string): void {
Iif (this.verbosity === Verbosity.Quiet) {
return;
}
Iif (this.mode.isTTY) {
this.log(`${chalk.green(ICON_CHECK)} ${message}`);
} else {
this.logPlain(message);
}
}
/**
* Log a file creation message (green "Created" prefix, no icon).
*/
created(filename: string): void {
if (this.verbosity === Verbosity.Quiet) {
return;
}
if (this.mode.isTTY) {
this.log(`${chalk.green('Created')} ${filename}`);
} else {
this.logPlain(`Created ${filename}`);
}
}
/**
* Log a skipped file message (yellow "Skipped" prefix with reason).
*/
skipped(filename: string, reason?: string): void {
if (this.verbosity === Verbosity.Quiet) {
return;
}
const suffix = reason ? chalk.dim(` (${reason})`) : '';
if (this.mode.isTTY) {
this.log(`${chalk.yellow('Skipped')} ${filename}${suffix}`);
} else {
this.logPlain(`Skipped ${filename}${reason ? ` (${reason})` : ''}`);
}
}
/**
* Log a warning message.
*/
warning(message: string): void {
if (this.verbosity === Verbosity.Quiet) {
return;
}
Iif (this.mode.isTTY) {
this.log(`${chalk.yellow(figures.warning)} ${message}`);
} else {
this.logPlain(`WARN: ${message}`);
}
}
/**
* Log an error message.
* Errors are always shown, even in quiet mode.
*/
error(message: string): void {
Iif (this.mode.isTTY) {
console.error(`${chalk.red(figures.cross)} ${message}`);
} else {
console.error(`[${timestamp()}] warden: ERROR: ${message}`);
}
}
/**
* Log a debug message.
*/
debug(message: string): void {
Eif (this.verbosity < Verbosity.Debug) {
return;
}
if (this.mode.isTTY) {
this.log(chalk.dim(`[debug] ${message}`));
} else {
this.logPlain(`DEBUG: ${message}`);
}
}
/**
* Log a hint/tip message.
*/
tip(message: string): void {
if (this.verbosity === Verbosity.Quiet) {
return;
}
Iif (this.mode.isTTY) {
this.log(chalk.dim(`Tip: ${message}`));
}
// No tips in CI mode
}
/**
* Log dim/subtle text (visible at normal verbosity, hidden in quiet mode).
*/
dim(message: string): void {
Iif (this.verbosity === Verbosity.Quiet) {
return;
}
Iif (this.mode.isTTY) {
this.log(chalk.dim(message));
} else {
this.logPlain(message);
}
}
/**
* Log plain text (no prefix).
*/
text(message: string): void {
if (this.verbosity === Verbosity.Quiet) {
return;
}
if (this.mode.isTTY) {
this.log(message);
} else {
this.logPlain(message);
}
}
/**
* Log bold text.
*/
bold(message: string): void {
Iif (this.verbosity === Verbosity.Quiet) {
return;
}
Iif (this.mode.isTTY) {
this.log(chalk.bold(message));
} else {
this.logPlain(message);
}
}
/**
* Output a blank line.
*/
blank(): void {
if (this.verbosity === Verbosity.Quiet) {
return;
}
this.log('');
}
/**
* Render an empty state box (e.g., "No changes found").
*/
renderEmptyState(message: string, tip?: string): void {
if (this.verbosity === Verbosity.Quiet) {
console.log(message);
return;
}
if (this.mode.isTTY) {
const box = new BoxRenderer({
title: 'warden',
mode: this.mode,
});
box.header();
box.blank();
box.content(`${chalk.yellow(figures.warning)} ${message}`);
if (tip) {
box.blank();
box.content(chalk.dim(`Tip: ${tip}`));
}
box.blank();
box.footer();
for (const line of box.render()) {
this.log(line);
}
} else {
this.logPlain(`WARN: ${message}`);
if (tip) {
this.logPlain(`Tip: ${tip}`);
}
}
}
}
|