File size: 24,531 Bytes
3487f22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
"""
Braintrust Evaluations for the Contract Analysis Pipeline.

Evaluates the full LangGraph pipeline (ingestion β†’ classification β†’ risk_analysis β†’
benchmark β†’ report) using:
- Braintrust Eval() framework
- Autoevals LLM-as-judge scorers (Factuality, ClosedQA) via Claude Sonnet 4.6
- Custom heuristic scorers for each agent stage's output validity

Usage:
    python agent-evaluation/eval.py
    python agent-evaluation/eval.py --dataset eval_dataset.json --output eval_metrics.json
    python agent-evaluation/eval.py --no-send-logs
    python agent-evaluation/eval.py --debug
"""

import argparse
import json
import logging
import os
import sys
import time
from pathlib import Path
from typing import Any, Optional

# Allow imports from the project root (agents/, etc.)
_PROJECT_ROOT = Path(__file__).resolve().parent.parent
if str(_PROJECT_ROOT) not in sys.path:
    sys.path.insert(0, str(_PROJECT_ROOT))

import braintrust
from autoevals.llm import Factuality
from braintrust import Eval
from dotenv import load_dotenv
from openai import OpenAI


logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s,p%(process)s,{%(filename)s:%(lineno)d},%(levelname)s,%(message)s",
)
logger = logging.getLogger(__name__)

os.chdir(_PROJECT_ROOT)
load_dotenv()

_EVAL_DIR = Path(__file__).resolve().parent
DEFAULT_DATASET_PATH = str(_EVAL_DIR / "eval_dataset.json")
DEFAULT_OUTPUT_PATH = str(_EVAL_DIR / "eval_metrics.json")
BRAINTRUST_PROJECT_NAME = os.environ.get("BRAINTRUST_PROJECT", "contract-pipeline-evals")

# Side-channel cache populated by the wrapped scorer so _export_eval_metrics
# can persist expected/found/matched type breakdowns to eval_metrics.json.
_clause_type_metadata: dict[str, dict] = {}
EVAL_JUDGE_MODEL = "claude-sonnet-4-6"
ANTHROPIC_OPENAI_BASE_URL = "https://api.anthropic.com/v1/"

braintrust.auto_instrument()
braintrust.init_logger(
    api_key=os.environ.get("BRAINTRUST_API_KEY"),
    project=BRAINTRUST_PROJECT_NAME,
)


def _create_judge_client() -> OpenAI:
    """
    Create an OpenAI-compatible client pointing at Anthropic's API.

    Autoevals scorers use the OpenAI SDK interface; Anthropic's compatible
    endpoint lets us use Claude Sonnet 4.6 as the judge model.
    """
    api_key = os.getenv("ANTHROPIC_API_KEY")
    if not api_key:
        raise ValueError("ANTHROPIC_API_KEY not set in environment")
    return OpenAI(api_key=api_key, base_url=ANTHROPIC_OPENAI_BASE_URL)


def _load_dataset(dataset_path: str) -> list[dict]:
    path = Path(dataset_path)
    if not path.exists():
        raise FileNotFoundError(f"Dataset not found: {dataset_path}")
    with open(path) as f:
        dataset = json.load(f)
    logger.info(f"Loaded {len(dataset)} test cases from {dataset_path}")
    return dataset


def _run_pipeline_on_input(contract_text: str) -> dict:
    """
    Run the full LangGraph pipeline on a contract and return all intermediate
    state plus timing information.
    """
    from agents.orchestrator_agent import run_pipeline

    logger.info(f"Running pipeline on contract ({len(contract_text)} chars)...")
    start = time.time()

    try:
        result = run_pipeline(contract_text)
        elapsed = time.time() - start
        report = json.loads(result["report"])
    except Exception as exc:
        elapsed = time.time() - start
        logger.error(f"Pipeline failed: {exc}")
        return {
            "pipeline_error": str(exc),
            "report": None,
            "clauses": [],
            "classified_clauses": [],
            "risk_scores": [],
            "benchmark_results": [],
            "latency_seconds": elapsed,
        }

    return {
        "pipeline_error": None,
        "report": report,
        "clauses": result.get("clauses", []),
        "classified_clauses": result.get("classified_clauses", []),
        "risk_scores": result.get("risk_scores", []),
        "benchmark_results": result.get("benchmark_results", []),
        "latency_seconds": elapsed,
    }


# ---------------------------------------------------------------------------
# Custom Scorers
# ---------------------------------------------------------------------------


def clause_structure_validity_scorer(
    input: str,
    output: str,
    expected: Optional[str] = None,
    metadata: Optional[dict] = None,
) -> Optional[dict]:
    """
    Ingestion agent: all parsed clauses must have non-empty id, text, and section.
    """
    if not metadata:
        return None

    clauses = metadata.get("clauses", [])
    if not clauses:
        return {
            "name": "ClauseStructureValidity",
            "score": 0.0,
            "metadata": {"reason": "no clauses produced by ingestion agent"},
        }

    required = {"id", "text", "section"}
    valid = sum(
        1 for c in clauses
        if required.issubset(c.keys()) and str(c.get("text", "")).strip()
    )
    score = valid / len(clauses)

    return {
        "name": "ClauseStructureValidity",
        "score": score,
        "metadata": {"total_clauses": len(clauses), "valid_clauses": valid},
    }


def classification_validity_scorer(
    input: str,
    output: str,
    expected: Optional[str] = None,
    metadata: Optional[dict] = None,
) -> Optional[dict]:
    """
    Classification agent: each clause must have a non-empty clause_type and a
    confidence value in [0, 1].
    """
    if not metadata:
        return None

    classified = metadata.get("classified_clauses", [])
    if not classified:
        return {
            "name": "ClassificationValidity",
            "score": 0.0,
            "metadata": {"reason": "no classified clauses"},
        }

    valid = sum(
        1 for c in classified
        if c.get("clause_type", "") and 0.0 <= c.get("confidence", -1) <= 1.0
    )
    score = valid / len(classified)

    return {
        "name": "ClassificationValidity",
        "score": score,
        "metadata": {"total": len(classified), "valid": valid},
    }


def expected_clause_type_scorer(
    input: str,
    output: str,
    expected: Optional[str] = None,
    metadata: Optional[dict] = None,
) -> Optional[dict]:
    """
    Classification agent: fraction of expected CUAD clause types found in
    the classified output. Penalises missing types; does not penalise extras.
    """
    if not metadata:
        return None

    expected_types = metadata.get("expected_clause_types", [])
    classified = metadata.get("classified_clauses", [])
    if not expected_types or not classified:
        return None

    found = {c.get("clause_type", "") for c in classified}
    expected_set = set(expected_types)
    matched = expected_set & found
    score = len(matched) / len(expected_set)

    return {
        "name": "ExpectedClauseType",
        "score": score,
        "metadata": {
            "expected_types": sorted(expected_set),
            "found_types": sorted(found),
            "matched_types": sorted(matched),
        },
    }


def _expected_clause_type_scorer_with_cache(
    input: str,
    output: str,
    expected: Optional[str] = None,
    metadata: Optional[dict] = None,
) -> Optional[dict]:
    result = expected_clause_type_scorer(input, output, expected, metadata)
    if result and result.get("metadata"):
        _clause_type_metadata[input] = result["metadata"]
    return result


def risk_score_validity_scorer(
    input: str,
    output: str,
    expected: Optional[str] = None,
    metadata: Optional[dict] = None,
) -> Optional[dict]:
    """
    Risk analysis agent: risk_score must be in [0, 1] and risk_factors must be a list.
    """
    if not metadata:
        return None

    risk_scores = metadata.get("risk_scores", [])
    if not risk_scores:
        return {
            "name": "RiskScoreValidity",
            "score": 0.0,
            "metadata": {"reason": "no risk scores produced"},
        }

    valid = sum(
        1 for c in risk_scores
        if 0.0 <= c.get("risk_score", -1) <= 1.0
        and isinstance(c.get("risk_factors"), list)
    )
    score = valid / len(risk_scores)

    return {
        "name": "RiskScoreValidity",
        "score": score,
        "metadata": {"total": len(risk_scores), "valid": valid},
    }


def risk_factors_presence_scorer(
    input: str,
    output: str,
    expected: Optional[str] = None,
    metadata: Optional[dict] = None,
) -> Optional[dict]:
    """
    Risk analysis agent: high-risk clauses (score >= 0.5) should have at least one
    identified risk factor, not an empty list.
    """
    if not metadata:
        return None

    risk_scores = metadata.get("risk_scores", [])
    high_risk = [c for c in risk_scores if c.get("risk_score", 0) >= 0.5]
    if not high_risk:
        return None

    with_factors = sum(1 for c in high_risk if c.get("risk_factors", []))
    score = with_factors / len(high_risk)

    return {
        "name": "RiskFactorsPresence",
        "score": score,
        "metadata": {"high_risk_clauses": len(high_risk), "with_factors": with_factors},
    }


def benchmark_similarity_validity_scorer(
    input: str,
    output: str,
    expected: Optional[str] = None,
    metadata: Optional[dict] = None,
) -> Optional[dict]:
    """
    Benchmark agent: benchmark_similarity must be in [0, 1] and a source must be
    provided for every clause.
    """
    if not metadata:
        return None

    benchmark = metadata.get("benchmark_results", [])
    if not benchmark:
        return {
            "name": "BenchmarkSimilarityValidity",
            "score": 0.0,
            "metadata": {"reason": "no benchmark results produced"},
        }

    valid = sum(
        1 for c in benchmark
        if 0.0 <= c.get("benchmark_similarity", -1) <= 1.0
        and c.get("benchmark_source", "")
    )
    score = valid / len(benchmark)

    return {
        "name": "BenchmarkSimilarityValidity",
        "score": score,
        "metadata": {"total": len(benchmark), "valid": valid},
    }


def output_structure_validity_scorer(
    input: str,
    output: str,
    expected: Optional[str] = None,
    metadata: Optional[dict] = None,
) -> Optional[dict]:
    """
    Report node: the final JSON report must contain a summary with total_clauses,
    a non-empty clauses list, and each clause must have the required keys.
    """
    if not metadata:
        return None

    report = metadata.get("report")
    if report is None:
        return {
            "name": "OutputStructureValidity",
            "score": 0.0,
            "metadata": {"reason": "no report produced"},
        }

    checks = {
        "has_summary": "summary" in report,
        "has_clauses_key": "clauses" in report,
        "has_total_clauses": "total_clauses" in report.get("summary", {}),
        "clauses_is_list": isinstance(report.get("clauses"), list),
        "clauses_not_empty": len(report.get("clauses", [])) > 0,
    }

    required_clause_keys = {"id", "clause_type", "risk_score", "benchmark_similarity"}
    checks["clause_keys_valid"] = all(
        required_clause_keys.issubset(c.keys())
        for c in report.get("clauses", [{}])
    )

    score = sum(checks.values()) / len(checks)

    return {
        "name": "OutputStructureValidity",
        "score": score,
        "metadata": checks,
    }


def latency_scorer(
    input: str,
    output: str,
    expected: Optional[str] = None,
    metadata: Optional[dict] = None,
) -> Optional[dict]:
    """
    Pipeline latency scorer. Thresholds are tuned for a multi-agent pipeline
    that makes ~3 sequential LLM calls per clause.

    < 30s  β†’ 1.0   (fast)
    30–60s β†’ 0.75
    60–120sβ†’ 0.5
    120–180sβ†’ 0.25
    > 180s β†’ 0.0   (too slow)
    """
    if not metadata:
        return None

    latency = metadata.get("latency_seconds")
    if latency is None:
        return None

    if latency < 30:
        score = 1.0
    elif latency < 60:
        score = 0.75
    elif latency < 120:
        score = 0.5
    elif latency < 180:
        score = 0.25
    else:
        score = 0.0

    return {
        "name": "Latency",
        "score": score,
        "metadata": {"latency_seconds": round(latency, 2)},
    }


def no_error_scorer(
    input: str,
    output: str,
    expected: Optional[str] = None,
    metadata: Optional[dict] = None,
) -> Optional[dict]:
    """
    Pipeline-level: score 1.0 if the pipeline completed without raising an
    exception and produced a non-None report.
    """
    if not metadata:
        return None

    error = metadata.get("pipeline_error")
    if error:
        return {
            "name": "NoError",
            "score": 0.0,
            "metadata": {"error": str(error)[:200]},
        }

    if metadata.get("report") is None:
        return {
            "name": "NoError",
            "score": 0.0,
            "metadata": {"reason": "no report produced"},
        }

    return {"name": "NoError", "score": 1.0, "metadata": {}}


# ---------------------------------------------------------------------------
# Task Function and Data Loader
# ---------------------------------------------------------------------------


def _create_wrapped_task(dataset: list[dict]):
    """
    Run the pipeline inside data() and cache results so that runtime metadata
    (per-agent outputs, latency) is available to scorers via the metadata dict.

    This mirrors the lab10 pattern: Braintrust passes metadata from data() to
    every scorer, but pipeline outputs are only known at runtime β€” so we run
    the pipeline here and inject the results into metadata.
    """
    results_cache: dict[str, dict] = {}

    def data() -> list[dict]:
        cases = []
        for case in dataset:
            contract_text = case["input"]
            category = case.get("category", "unknown")
            logger.info(f"Running pipeline for test case: [{category}]")

            result = _run_pipeline_on_input(contract_text)
            results_cache[contract_text] = result

            cases.append({
                "input": contract_text,
                "expected": case.get("expected_output", ""),
                "metadata": {
                    "category": case.get("category", ""),
                    "difficulty": case.get("difficulty", ""),
                    "expected_clause_types": case.get("expected_clause_types", []),
                    # Runtime outputs injected for custom scorers
                    "clauses": result.get("clauses", []),
                    "classified_clauses": result.get("classified_clauses", []),
                    "risk_scores": result.get("risk_scores", []),
                    "benchmark_results": result.get("benchmark_results", []),
                    "report": result.get("report"),
                    "pipeline_error": result.get("pipeline_error"),
                    "latency_seconds": result.get("latency_seconds"),
                },
            })

        return cases

    def task(input: str) -> str:
        # Pipeline already ran in data(); return the cached report JSON.
        if input in results_cache:
            report = results_cache[input].get("report")
            return json.dumps(report, indent=2) if report else "Pipeline failed β€” no report produced"
        # Fallback if cache miss (should not happen in normal eval flow)
        result = _run_pipeline_on_input(input)
        report = result.get("report")
        return json.dumps(report, indent=2) if report else "Pipeline failed β€” no report produced"

    return task, data


# ---------------------------------------------------------------------------
# Reporting
# ---------------------------------------------------------------------------


def _print_eval_summary(eval_result: Any, dataset: list[dict]) -> None:
    results = eval_result.results
    if not results:
        logger.warning("No evaluation results to summarize")
        return

    category_lookup = {case["input"]: case.get("category", "unknown") for case in dataset}

    scorer_scores: dict[str, list[float]] = {}
    category_scores: dict[str, list[float]] = {}
    error_cases = []

    for r in results:
        input_text = str(r.input) if r.input else ""
        category = category_lookup.get(input_text, "unknown")

        if r.error:
            error_cases.append({"input": input_text[:80], "error": str(r.error)})
            continue

        for scorer_name, score_val in r.scores.items():
            if score_val is None:
                continue
            scorer_scores.setdefault(scorer_name, []).append(score_val)
            category_scores.setdefault(f"{category}/{scorer_name}", []).append(score_val)

    print("\n" + "=" * 80)
    print("EVALUATION SUMMARY β€” CONTRACT ANALYSIS PIPELINE")
    print("=" * 80)
    print(f"Total test cases : {len(results)}")
    print(f"Errors           : {len(error_cases)}")
    print()

    print("-" * 80)
    print(f"{'Scorer':<35} {'Avg':>10} {'Min':>8} {'Max':>8} {'N':>6}")
    print("-" * 80)
    for name in sorted(scorer_scores):
        scores = scorer_scores[name]
        avg = sum(scores) / len(scores)
        print(f"{name:<35} {avg:>10.2%} {min(scores):>8.2f} {max(scores):>8.2f} {len(scores):>6}")

    print()
    print("-" * 80)
    print("PER-CATEGORY BREAKDOWN")
    print("-" * 80)
    categories = sorted({case.get("category", "unknown") for case in dataset})
    for cat in categories:
        print(f"\n  [{cat}]")
        for name in sorted(scorer_scores):
            key = f"{cat}/{name}"
            if key in category_scores:
                scores = category_scores[key]
                print(f"    {name:<33} {sum(scores)/len(scores):>8.2%}  (n={len(scores)})")

    if error_cases:
        print()
        print("-" * 80)
        print("FAILED CASES")
        print("-" * 80)
        for case in error_cases:
            print(f"  Input: {case['input']}")
            print(f"  Error: {case['error']}")
            print()

    print("=" * 80 + "\n")
    logger.info(
        f"Eval summary: {len(results)} cases, {len(error_cases)} errors β€” "
        + ", ".join(
            f"{k}={sum(v)/len(v):.2%}"
            for k, v in sorted(scorer_scores.items())
        )
    )


def _export_eval_metrics(
    eval_result: Any,
    dataset: list[dict],
    output_path: str = DEFAULT_OUTPUT_PATH,
) -> None:
    results = eval_result.results
    if not results:
        logger.warning("No results to export")
        return

    category_lookup = {case["input"]: case.get("category", "unknown") for case in dataset}

    scorer_scores: dict[str, list[float]] = {}
    category_scores: dict[str, list[float]] = {}
    per_case_results = []
    error_count = 0

    for r in results:
        input_text = str(r.input) if r.input else ""
        category = category_lookup.get(input_text, "unknown")

        clause_type_meta = _clause_type_metadata.get(input_text)
        case_entry: dict = {
            "input_preview": input_text[:120],
            "category": category,
            "scores": {},
            "scorer_metadata": {
                "ExpectedClauseType": clause_type_meta,
            } if clause_type_meta else {},
            "error": None,
        }

        if r.error:
            error_count += 1
            case_entry["error"] = str(r.error)
            per_case_results.append(case_entry)
            continue

        for scorer_name, score_val in r.scores.items():
            if score_val is None:
                continue
            case_entry["scores"][scorer_name] = round(score_val, 4)
            scorer_scores.setdefault(scorer_name, []).append(score_val)
            category_scores.setdefault(f"{category}/{scorer_name}", []).append(score_val)

        per_case_results.append(case_entry)

    overall = {
        name: {
            "average": round(sum(scores) / len(scores), 4),
            "min": round(min(scores), 4),
            "max": round(max(scores), 4),
            "count": len(scores),
        }
        for name, scores in sorted(scorer_scores.items())
    }

    categories = sorted({case.get("category", "unknown") for case in dataset})
    per_category: dict[str, dict] = {}
    for cat in categories:
        per_category[cat] = {}
        for name in sorted(scorer_scores):
            key = f"{cat}/{name}"
            if key in category_scores:
                scores = category_scores[key]
                per_category[cat][name] = {
                    "average": round(sum(scores) / len(scores), 4),
                    "count": len(scores),
                }

    metrics = {
        "total_cases": len(results),
        "errors": error_count,
        "overall_scores": overall,
        "per_category": per_category,
        "per_case": per_case_results,
    }

    with open(output_path, "w") as f:
        json.dump(metrics, f, indent=2, default=str)

    logger.info(f"Evaluation metrics exported to {output_path}")


# ---------------------------------------------------------------------------
# CLI
# ---------------------------------------------------------------------------


def _parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(
        description="Run Braintrust evaluations on the Contract Analysis Pipeline",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""
Example usage:
    python agent-evaluation/eval.py
    python agent-evaluation/eval.py --dataset eval_dataset.json --output eval_metrics.json
    python agent-evaluation/eval.py --no-send-logs
    python agent-evaluation/eval.py --debug
""",
    )
    parser.add_argument(
        "--dataset", type=str, default=DEFAULT_DATASET_PATH,
        help=f"Path to evaluation dataset JSON (default: {DEFAULT_DATASET_PATH})",
    )
    parser.add_argument(
        "--output", type=str, default=DEFAULT_OUTPUT_PATH,
        help=f"Path for output eval metrics JSON (default: {DEFAULT_OUTPUT_PATH})",
    )
    parser.add_argument(
        "--no-send-logs", action="store_true",
        help="Run evaluations locally without sending results to Braintrust",
    )
    parser.add_argument(
        "--experiment-name", type=str, default=None,
        help="Name for this evaluation experiment (default: auto-generated)",
    )
    parser.add_argument(
        "--debug", action="store_true",
        help="Enable debug logging",
    )
    return parser.parse_args()


def main() -> None:
    args = _parse_args()
    if args.debug:
        logging.getLogger().setLevel(logging.DEBUG)

    logger.info("Starting Contract Analysis Pipeline Evaluations")
    start = time.time()

    dataset = _load_dataset(args.dataset)
    task_fn, data_fn = _create_wrapped_task(dataset)

    # Use Claude Sonnet 4.6 as the judge model via Anthropic's OpenAI-compatible endpoint
    judge_client = _create_judge_client()

    # Scorers
    # LLM-as-judge (Factuality, ClosedQA) β€” assess overall report quality vs expected
    # Heuristic per-agent scorers β€” validate structure and field validity at each stage
    all_scorers = [
        Factuality(model=EVAL_JUDGE_MODEL, client=judge_client),
        clause_structure_validity_scorer,       # ingestion agent
        classification_validity_scorer,         # classification agent
        _expected_clause_type_scorer_with_cache,  # classification agent
        risk_score_validity_scorer,             # risk analysis agent
        risk_factors_presence_scorer,           # risk analysis agent
        benchmark_similarity_validity_scorer,   # benchmark agent
        output_structure_validity_scorer,       # report node
        latency_scorer,                         # pipeline-level
        no_error_scorer,                        # pipeline-level
    ]

    eval_kwargs: dict[str, Any] = {
        "data": data_fn,
        "task": task_fn,
        "scores": all_scorers,
    }

    if args.experiment_name:
        eval_kwargs["experiment_name"] = args.experiment_name

    if args.no_send_logs:
        eval_kwargs["no_send_logs"] = True
        logger.info("Running in local mode (no logs sent to Braintrust)")

    logger.info("Running Braintrust evaluation...")
    eval_result = Eval(BRAINTRUST_PROJECT_NAME, **eval_kwargs)

    _print_eval_summary(eval_result, dataset)
    _export_eval_metrics(eval_result, dataset, output_path=args.output)

    elapsed = time.time() - start
    minutes = int(elapsed // 60)
    seconds = elapsed % 60
    if minutes > 0:
        logger.info(f"Evaluation completed in {minutes}m {seconds:.1f}s")
    else:
        logger.info(f"Evaluation completed in {seconds:.1f}s")


if __name__ == "__main__":
    main()