File size: 2,284 Bytes
38be44d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env tsx

/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

/**
 * @fileoverview CLI entry point to summarize eval report.json files.
 *
 * Scans a directory for report.json files, groups them by model name,
 * and prints pass rate summaries. Integrates with static inventory data
 * to display static policies.
 *
 * Usage:
 *   npm run eval:report
 *   npm run eval:report -- <reports-directory> [--json] [--root <repo-root>]
 */

import path from 'node:path';
import fs from 'node:fs';
import { collectInventory } from './utils/eval-inventory.js';
import {
  summarizeReports,
  formatReportSummary,
  formatReportSummaryJson,
} from './utils/eval-report.js';

async function main() {
  const args = process.argv.slice(2);

  const jsonFlagIndex = args.indexOf('--json');
  const jsonMode = jsonFlagIndex !== -1;
  if (jsonMode) args.splice(jsonFlagIndex, 1);

  const rootFlagIndex = args.indexOf('--root');
  let repoRoot: string | undefined;
  if (rootFlagIndex !== -1) {
    repoRoot = args[rootFlagIndex + 1];
    if (repoRoot === undefined || repoRoot.startsWith('--')) {
      console.error('Error: --root requires a valid directory path.');
      process.exit(1);
    }
    args.splice(rootFlagIndex, 2);
  }

  const resolvedRoot = repoRoot ? path.resolve(repoRoot) : process.cwd();

  // The first positional argument is the directory of reports
  const reportsDirArg = args.find((a) => !a.startsWith('--'));
  const reportsDir = reportsDirArg
    ? path.resolve(reportsDirArg)
    : path.join(resolvedRoot, 'evals', 'logs');

  if (!fs.existsSync(reportsDir)) {
    console.error(`Error: Reports directory does not exist: ${reportsDir}`);
    process.exit(1);
  }

  // Try to load inventory if available to match policies
  let inventory;
  try {
    inventory = await collectInventory(resolvedRoot);
  } catch {
    // If inventory fails to load (e.g. running outside repo), proceed without it
  }

  const summary = await summarizeReports(reportsDir, inventory);

  if (jsonMode) {
    console.log(formatReportSummaryJson(summary, resolvedRoot));
  } else {
    console.log(formatReportSummary(summary, resolvedRoot));
  }
}

main().catch((error) => {
  console.error('Fatal error:', error);
  process.exit(1);
});