File size: 28,496 Bytes
569e668
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
#!/usr/bin/env python3
"""
run_benchmark.py — Invoice Validation Benchmark Harness
========================================================

Runs 200 synthetic invoices through language models via OpenAI-compatible APIs
(Ollama for local models, vLLM for remote) under two conditions:

  Condition B: Model extracts fields AND verifies arithmetic (engineered prompt)
  Condition C: Model extracts fields only; deterministic code verifies arithmetic

Outputs a results CSV with per-invoice scores, error bands, and metadata.

Requirements:
    pip install openai

Usage:
    # Run all invoices through one local model, both conditions
    python run_benchmark.py --models llama3.1:8b

    # Run specific models, specific condition
    python run_benchmark.py --models llama3.1:8b,qwen3:8b --conditions B

    # Test on a single invoice first (sanity check)
    python run_benchmark.py --models llama3.1:8b --invoice INV-2026-0001

    # Remote models via vLLM (prefix with vllm:)
    python run_benchmark.py --models vllm:meta-llama/Llama-3.1-8B-Instruct --vllm-url http://gpu-server:8000/v1

    # Resume an interrupted run (skips existing results)
    python run_benchmark.py --models llama3.1:8b --resume

    # Dry run — no API calls, just test scoring on synthetic responses
    python run_benchmark.py --dry-run
"""

import argparse
import csv
import json
import os
import re
import sys
import time
from dataclasses import dataclass, field, asdict
from datetime import datetime, timezone
from decimal import Decimal, ROUND_HALF_UP, InvalidOperation
from pathlib import Path
from typing import Optional

try:
    from openai import OpenAI
except ImportError:
    print("ERROR: openai package required. Run: pip install openai")
    sys.exit(1)


# ---------------------------------------------------------------------------
# Paths (relative to this script's directory)
# ---------------------------------------------------------------------------
SCRIPT_DIR = Path(__file__).resolve().parent
INVOICE_DIR = SCRIPT_DIR / "output" / "invoices"
GROUND_TRUTH_DIR = SCRIPT_DIR / "output" / "ground_truth"
PROMPT_DIR = SCRIPT_DIR / "prompts"
RESULTS_DIR = SCRIPT_DIR / "results"
MANIFEST_PATH = SCRIPT_DIR / "output" / "manifest.csv"


# ---------------------------------------------------------------------------
# Error bands
# ---------------------------------------------------------------------------
BAND_EXACT = "exact"
BAND_GHOST = "ghost"            # <1% — undetectable without recalculation
BAND_PLAUSIBLE = "plausible"    # 1–5% — the most dangerous band
BAND_SUSPICIOUS = "suspicious"  # 5–15% — a competent reviewer catches this
BAND_OBVIOUS = "obvious"        # >15% — everyone notices
BAND_PARSE_ERROR = "parse_error"


# ---------------------------------------------------------------------------
# CSV column order
# ---------------------------------------------------------------------------
RESULT_FIELDS = [
    "invoice_id", "model", "condition", "timestamp",
    # Ground truth
    "true_total", "rendered_total", "is_consistent", "number_format",
    "vat_variant", "discount_variant", "layout", "edge_case",
    # Model output
    "model_total", "model_raw_total",
    # Scoring
    "error_amount", "error_pct", "error_band", "exact_match",
    # Condition B extras
    "model_flagged_inconsistency", "model_is_consistent",
    "model_assumptions",
    # Condition C extras
    "recomputed_total",
    # Meta
    "response_time_s", "parse_success", "raw_response_length",
    "error_message",
]


# ---------------------------------------------------------------------------
# Data loading
# ---------------------------------------------------------------------------

def load_manifest() -> dict[str, dict]:
    """Load manifest.csv into a dict keyed by invoice_id."""
    manifest = {}
    with open(MANIFEST_PATH, newline="") as f:
        for row in csv.DictReader(f):
            manifest[row["id"]] = row
    return manifest


def load_invoice_text(invoice_id: str) -> str:
    """Load the markdown text of an invoice."""
    path = INVOICE_DIR / f"{invoice_id}.md"
    return path.read_text(encoding="utf-8")


def load_ground_truth(invoice_id: str) -> dict:
    """Load the ground truth JSON for an invoice."""
    path = GROUND_TRUTH_DIR / f"{invoice_id}.json"
    return json.loads(path.read_text(encoding="utf-8"))


def load_prompt_template(condition: str) -> str:
    """Load the prompt template for a condition (B or C)."""
    filename = {
        "B": "condition_b_engineered.txt",
        "C": "condition_c_extraction.txt",
    }[condition]
    return (PROMPT_DIR / filename).read_text(encoding="utf-8")


def list_invoice_ids() -> list[str]:
    """List all invoice IDs from the manifest."""
    manifest = load_manifest()
    return sorted(manifest.keys())


def load_completed_results(results_path: Path) -> set[tuple[str, str, str]]:
    """Load (invoice_id, model, condition) tuples from an existing results CSV."""
    completed = set()
    if results_path.exists():
        with open(results_path, newline="") as f:
            for row in csv.DictReader(f):
                completed.add((row["invoice_id"], row["model"], row["condition"]))
    return completed


# ---------------------------------------------------------------------------
# Response parsing
# ---------------------------------------------------------------------------

def extract_json_from_response(text: str) -> Optional[dict]:
    """
    Extract a JSON object from model response text.
    Handles: raw JSON, markdown fences (```json ... ```), leading/trailing text,
    and reasoning model output wrapped in <think>...</think> tags.
    """
    if not text or not text.strip():
        return None

    # Strip reasoning model thinking traces (Qwen3, DeepSeek-R1, QwQ, etc.)
    # These wrap chain-of-thought in <think>...</think> before the actual answer.
    # The think block may contain JSON-like snippets that would confuse the parser.
    # First: strip closed <think>...</think> blocks
    text = re.sub(r"<think>.*?</think>", "", text, flags=re.DOTALL).strip()
    # Second: strip unclosed <think> tags (model sometimes streams thinking without closing)
    text = re.sub(r"<think>.*", "", text, flags=re.DOTALL).strip()

    # Try 1: strip markdown fences
    fenced = re.search(r"```(?:json)?\s*\n?(.*?)\n?\s*```", text, re.DOTALL)
    if fenced:
        try:
            return json.loads(fenced.group(1))
        except json.JSONDecodeError:
            pass

    # Try 2: find the outermost { ... } block
    # (handles models that add explanation before/after the JSON)
    brace_start = text.find("{")
    if brace_start == -1:
        return None

    # Walk forward to find matching closing brace
    depth = 0
    in_string = False
    escape_next = False
    for i in range(brace_start, len(text)):
        c = text[i]
        if escape_next:
            escape_next = False
            continue
        if c == "\\":
            escape_next = True
            continue
        if c == '"' and not escape_next:
            in_string = not in_string
            continue
        if in_string:
            continue
        if c == "{":
            depth += 1
        elif c == "}":
            depth -= 1
            if depth == 0:
                try:
                    return json.loads(text[brace_start : i + 1])
                except json.JSONDecodeError:
                    return None

    # Try 3: brute force
    try:
        return json.loads(text.strip())
    except json.JSONDecodeError:
        return None


# ---------------------------------------------------------------------------
# Error classification
# ---------------------------------------------------------------------------

def classify_error(error_pct: float) -> str:
    """Classify absolute error percentage into scoring bands."""
    abs_pct = abs(error_pct)
    if abs_pct == 0:
        return BAND_EXACT
    elif abs_pct < 1.0:
        return BAND_GHOST
    elif abs_pct < 5.0:
        return BAND_PLAUSIBLE
    elif abs_pct < 15.0:
        return BAND_SUSPICIOUS
    else:
        return BAND_OBVIOUS


def compute_error(model_total: Decimal, true_total: Decimal) -> tuple[Decimal, float, str]:
    """
    Returns (error_amount, error_pct, error_band).
    error_pct is relative to true_total. For zero true_total, any nonzero model_total is 100%.
    """
    error_amount = model_total - true_total
    if true_total == 0:
        error_pct = 100.0 if error_amount != 0 else 0.0
    else:
        error_pct = float((error_amount / true_total) * 100)
    band = classify_error(error_pct)
    return error_amount, error_pct, band


# ---------------------------------------------------------------------------
# Decimal helpers
# ---------------------------------------------------------------------------

D = Decimal

def to_decimal(value) -> Decimal:
    """Convert a value to Decimal, handling strings, ints, floats, None."""
    if value is None:
        return D("0.00")
    if isinstance(value, Decimal):
        return value
    if isinstance(value, (int, float)):
        # Round floats to 2 decimal places to avoid float precision issues
        return D(str(value)).quantize(D("0.01"), rounding=ROUND_HALF_UP)
    if isinstance(value, str):
        value = value.strip().replace(",", "").replace(" ", "")
        try:
            return D(value).quantize(D("0.01"), rounding=ROUND_HALF_UP)
        except InvalidOperation:
            return D("0.00")
    return D("0.00")


def decimal_round(value: Decimal) -> Decimal:
    """Round to 2 decimal places."""
    return value.quantize(D("0.01"), rounding=ROUND_HALF_UP)


# ---------------------------------------------------------------------------
# Condition C: Recompute total from extracted fields
# ---------------------------------------------------------------------------

def recompute_total_from_extraction(extracted: dict) -> Decimal:
    """
    Compute the invoice total from model-extracted fields using Decimal arithmetic.

    Logic:
    - Sum line item amounts → computed_subtotal
    - If VAT is included in prices:
        total = computed_subtotal - discount
        (VAT is already baked into the line item amounts)
    - If VAT is excluded:
        vat = computed_subtotal * vat_rate
        total = computed_subtotal + vat - discount
    - If vat_rate is null (mixed VAT), fall back to stated_vat_amount
    - Conditional discounts (trade terms) are NOT applied
    """
    # Step 1: Sum line items
    computed_subtotal = D("0.00")
    line_items = extracted.get("line_items", [])
    for item in line_items:
        amount = item.get("amount")
        if amount is not None:
            computed_subtotal += to_decimal(amount)
        else:
            # Fall back to qty * unit_price
            qty = to_decimal(item.get("quantity", 0))
            price = to_decimal(item.get("unit_price", 0))
            computed_subtotal += decimal_round(qty * price)

    # Step 2: Determine VAT handling
    vat_included = extracted.get("vat_included_in_prices", False)
    vat_rate_raw = extracted.get("vat_rate")
    stated_vat = extracted.get("stated_vat_amount")

    # Step 3: Compute VAT
    if vat_included:
        # VAT is already in the line item amounts — don't add it again
        vat_amount = D("0.00")
    elif vat_rate_raw is not None:
        vat_rate = to_decimal(vat_rate_raw)
        vat_amount = decimal_round(computed_subtotal * vat_rate)
    elif stated_vat is not None:
        # Mixed VAT or rate not stated — use model's extracted VAT amount
        vat_amount = to_decimal(stated_vat)
    else:
        vat_amount = D("0.00")

    # Step 4: Compute discount
    discount_amount = D("0.00")
    is_conditional = extracted.get("discount_is_conditional", False)

    if not is_conditional:
        discount_raw = extracted.get("discount_amount")
        if discount_raw is not None and discount_raw != 0:
            discount_amount = to_decimal(discount_raw)
        elif extracted.get("discount_rate") is not None:
            rate = to_decimal(extracted["discount_rate"])
            discount_amount = decimal_round(computed_subtotal * rate)

    # Step 5: Compute total
    total = computed_subtotal + vat_amount - discount_amount
    return decimal_round(total)


# ---------------------------------------------------------------------------
# Scoring
# ---------------------------------------------------------------------------

def score_condition_b(parsed: dict, ground_truth: dict) -> dict:
    """
    Score a Condition B response.
    Compares model's reported total against ground truth correct total.
    Also checks whether the model detected inconsistencies.
    """
    true_total = to_decimal(ground_truth["total"])
    model_total_raw = parsed.get("total")
    model_total = to_decimal(model_total_raw)

    error_amount, error_pct, band = compute_error(model_total, true_total)

    # Did the model flag inconsistencies?
    is_consistent_gt = ground_truth["rendered_total"] == ground_truth["total"]
    model_flagged = bool(parsed.get("inconsistencies"))
    model_says_consistent = parsed.get("is_consistent", True)

    assumptions = parsed.get("assumptions", [])
    if isinstance(assumptions, list):
        assumptions = "; ".join(str(a) for a in assumptions)

    return {
        "model_total": str(model_total),
        "model_raw_total": str(model_total_raw),
        "error_amount": str(error_amount),
        "error_pct": f"{error_pct:.4f}",
        "error_band": band,
        "exact_match": str(error_amount == 0).lower(),
        "model_flagged_inconsistency": str(model_flagged).lower(),
        "model_is_consistent": str(model_says_consistent).lower(),
        "model_assumptions": assumptions,
        "recomputed_total": "",
    }


def score_condition_c(parsed: dict, ground_truth: dict) -> dict:
    """
    Score a Condition C response.
    Recomputes total from model-extracted fields, then compares against ground truth.
    """
    true_total = to_decimal(ground_truth["total"])

    # Recompute total from extracted fields
    recomputed = recompute_total_from_extraction(parsed)

    error_amount, error_pct, band = compute_error(recomputed, true_total)

    # Also capture what the model extracted as stated_total (for comparison)
    model_stated = parsed.get("stated_total")

    return {
        "model_total": str(recomputed),
        "model_raw_total": str(model_stated) if model_stated else "",
        "error_amount": str(error_amount),
        "error_pct": f"{error_pct:.4f}",
        "error_band": band,
        "exact_match": str(error_amount == 0).lower(),
        "model_flagged_inconsistency": "",
        "model_is_consistent": "",
        "model_assumptions": "",
        "recomputed_total": str(recomputed),
    }


# ---------------------------------------------------------------------------
# API calls
# ---------------------------------------------------------------------------

def get_client(model: str, ollama_url: str, vllm_url: Optional[str]) -> tuple[OpenAI, str]:
    """
    Return (OpenAI client, model_name) for a given model string.
    Models prefixed with 'vllm:' use the vLLM endpoint; others use Ollama.
    """
    if model.startswith("vllm:"):
        if not vllm_url:
            raise ValueError(f"Model {model} requires --vllm-url to be set")
        actual_model = model[5:]  # strip 'vllm:' prefix
        client = OpenAI(base_url=vllm_url, api_key="not-needed")
        return client, actual_model
    else:
        client = OpenAI(base_url=ollama_url, api_key="ollama")
        return client, model


def call_model(
    client: OpenAI,
    model_name: str,
    prompt: str,
    temperature: float = 0.0,
    max_retries: int = 2,
) -> tuple[str, float]:
    """
    Send prompt to model via OpenAI-compatible API.
    Returns (response_text, elapsed_seconds).
    Retries on transient errors.
    """
    for attempt in range(max_retries + 1):
        try:
            t0 = time.monotonic()
            response = client.chat.completions.create(
                model=model_name,
                messages=[{"role": "user", "content": prompt}],
                temperature=temperature,
                max_tokens=4096,
            )
            elapsed = time.monotonic() - t0
            text = response.choices[0].message.content or ""
            return text, elapsed
        except Exception as e:
            if attempt < max_retries:
                wait = 2 ** attempt
                print(f"    Retry {attempt + 1}/{max_retries} after error: {e}")
                time.sleep(wait)
            else:
                raise


# ---------------------------------------------------------------------------
# Single benchmark run
# ---------------------------------------------------------------------------

def run_single(
    client: OpenAI,
    model_name: str,
    model_label: str,
    invoice_id: str,
    condition: str,
    prompt_template: str,
    ground_truth: dict,
    manifest_row: dict,
    dry_run: bool = False,
) -> dict:
    """
    Run one invoice through one model under one condition.
    Returns a dict with all result fields.
    """
    # Build prompt
    invoice_text = load_invoice_text(invoice_id)
    prompt = prompt_template.replace("{invoice_text}", invoice_text)

    # Base result
    result = {
        "invoice_id": invoice_id,
        "model": model_label,
        "condition": condition,
        "timestamp": datetime.now(timezone.utc).isoformat(),
        "true_total": ground_truth["total"],
        "rendered_total": ground_truth.get("rendered_total", ground_truth["total"]),
        "is_consistent": str(ground_truth["rendered_total"] == ground_truth["total"]).lower(),
        "number_format": ground_truth["variants"]["number_format"],
        "vat_variant": ground_truth["variants"]["vat_variant"],
        "discount_variant": ground_truth["variants"]["discount_variant"],
        "layout": ground_truth["variants"]["layout"],
        "edge_case": ground_truth["variants"]["edge_case"],
    }

    if dry_run:
        # Return a placeholder result without calling the model
        result.update({
            "model_total": "", "model_raw_total": "",
            "error_amount": "", "error_pct": "", "error_band": "dry_run",
            "exact_match": "", "model_flagged_inconsistency": "",
            "model_is_consistent": "", "model_assumptions": "",
            "recomputed_total": "",
            "response_time_s": "0.0", "parse_success": "true",
            "raw_response_length": "0", "error_message": "",
        })
        return result

    # Call model
    try:
        response_text, elapsed = call_model(client, model_name, prompt)
    except Exception as e:
        result.update({
            "model_total": "", "model_raw_total": "",
            "error_amount": "", "error_pct": "", "error_band": BAND_PARSE_ERROR,
            "exact_match": "false", "model_flagged_inconsistency": "",
            "model_is_consistent": "", "model_assumptions": "",
            "recomputed_total": "",
            "response_time_s": "0.0", "parse_success": "false",
            "raw_response_length": "0",
            "error_message": f"API error: {e}",
        })
        return result

    result["response_time_s"] = f"{elapsed:.2f}"
    result["raw_response_length"] = str(len(response_text))

    # Parse response
    parsed = extract_json_from_response(response_text)
    if parsed is None:
        result.update({
            "model_total": "", "model_raw_total": "",
            "error_amount": "", "error_pct": "", "error_band": BAND_PARSE_ERROR,
            "exact_match": "false", "model_flagged_inconsistency": "",
            "model_is_consistent": "", "model_assumptions": "",
            "recomputed_total": "",
            "parse_success": "false",
            "error_message": f"JSON parse failed. First 200 chars: {response_text[:200]}",
        })
        return result

    result["parse_success"] = "true"
    result["error_message"] = ""

    # Score
    try:
        if condition == "B":
            scores = score_condition_b(parsed, ground_truth)
        else:
            scores = score_condition_c(parsed, ground_truth)
        result.update(scores)
    except Exception as e:
        result.update({
            "model_total": "", "model_raw_total": "",
            "error_amount": "", "error_pct": "", "error_band": BAND_PARSE_ERROR,
            "exact_match": "false", "model_flagged_inconsistency": "",
            "model_is_consistent": "", "model_assumptions": "",
            "recomputed_total": "",
            "error_message": f"Scoring error: {e}",
        })

    return result


# ---------------------------------------------------------------------------
# Progress display
# ---------------------------------------------------------------------------

def print_result_line(result: dict, index: int, total: int):
    """Print a compact one-line summary of a result."""
    band = result.get("error_band", "?")
    band_symbol = {
        BAND_EXACT: "  OK",
        BAND_GHOST: "  ~0",
        BAND_PLAUSIBLE: " 1-5",
        BAND_SUSPICIOUS: "5-15",
        BAND_OBVIOUS: " >15",
        BAND_PARSE_ERROR: " ERR",
        "dry_run": " DRY",
    }.get(band, "   ?")

    error_pct = result.get("error_pct", "")
    if error_pct:
        try:
            error_pct = f"{float(error_pct):+.2f}%"
        except ValueError:
            pass
    else:
        error_pct = "     "

    time_s = result.get("response_time_s", "0.0")

    print(
        f"  [{index:4d}/{total}] {result['invoice_id']}  "
        f"{result['condition']}  [{band_symbol}] {error_pct:>8s}  "
        f"{float(time_s):5.1f}s  {result['model']}"
    )


# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------

def main():
    parser = argparse.ArgumentParser(
        description="Invoice Validation Benchmark Harness",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog=__doc__,
    )
    parser.add_argument(
        "--models", required=True,
        help="Comma-separated model names. Prefix with 'vllm:' for remote models. "
             "E.g.: llama3.1:8b,qwen3:8b,vllm:meta-llama/Llama-3.1-8B-Instruct"
    )
    parser.add_argument(
        "--conditions", default="B,C",
        help="Comma-separated conditions to run (default: B,C)"
    )
    parser.add_argument(
        "--invoice", default=None,
        help="Run only this invoice ID (e.g. INV-2026-0001). Default: all 200."
    )
    parser.add_argument(
        "--ollama-url", default="http://localhost:11434/v1",
        help="Ollama API base URL (default: http://localhost:11434/v1)"
    )
    parser.add_argument(
        "--vllm-url", default=None,
        help="vLLM API base URL for remote models (e.g. http://gpu-server:8000/v1)"
    )
    parser.add_argument(
        "--temperature", type=float, default=0.0,
        help="Sampling temperature (default: 0.0 for deterministic output)"
    )
    parser.add_argument(
        "--output", default=None,
        help="Output CSV path. Default: results/benchmark_YYYY-MM-DD_HHMMSS.csv"
    )
    parser.add_argument(
        "--resume", action="store_true",
        help="Skip (invoice, model, condition) combos that already exist in the output file"
    )
    parser.add_argument(
        "--dry-run", action="store_true",
        help="Don't call models. Just verify data loading, prompt injection, and CSV writing."
    )
    parser.add_argument(
        "--delay", type=float, default=0.0,
        help="Seconds to wait between API calls (default: 0)"
    )

    args = parser.parse_args()

    # Parse arguments
    models = [m.strip() for m in args.models.split(",")]
    conditions = [c.strip().upper() for c in args.conditions.split(",")]
    for c in conditions:
        if c not in ("B", "C"):
            print(f"ERROR: Unknown condition '{c}'. Use B or C.")
            sys.exit(1)

    # Determine invoice list
    if args.invoice:
        invoice_ids = [args.invoice]
    else:
        invoice_ids = list_invoice_ids()

    # Load manifest and prompt templates
    manifest = load_manifest()
    prompt_templates = {}
    for c in conditions:
        prompt_templates[c] = load_prompt_template(c)

    # Determine output path
    RESULTS_DIR.mkdir(parents=True, exist_ok=True)
    if args.output:
        output_path = Path(args.output)
    else:
        ts = datetime.now().strftime("%Y-%m-%d_%H%M%S")
        output_path = RESULTS_DIR / f"benchmark_{ts}.csv"

    # Load existing results for resume
    completed = set()
    if args.resume and output_path.exists():
        completed = load_completed_results(output_path)
        print(f"Resume mode: {len(completed)} existing results found in {output_path}")

    # Build work list
    work = []
    for model_label in models:
        for condition in conditions:
            for inv_id in invoice_ids:
                if (inv_id, model_label, condition) in completed:
                    continue
                work.append((model_label, condition, inv_id))

    total_calls = len(work)
    print(f"\nInvoice Validation Benchmark")
    print(f"{'=' * 50}")
    print(f"Models:     {', '.join(models)}")
    print(f"Conditions: {', '.join(conditions)}")
    print(f"Invoices:   {len(invoice_ids)}")
    print(f"Total calls: {total_calls}" + (f" ({len(completed)} skipped)" if completed else ""))
    print(f"Output:     {output_path}")
    if args.dry_run:
        print(f"MODE:       DRY RUN (no API calls)")
    print(f"{'=' * 50}\n")

    if total_calls == 0:
        print("Nothing to do.")
        return

    # Open CSV (append if resuming, write header if new)
    write_header = not output_path.exists() or not args.resume
    csv_file = open(output_path, "a" if args.resume else "w", newline="", encoding="utf-8")
    writer = csv.DictWriter(csv_file, fieldnames=RESULT_FIELDS, extrasaction="ignore")
    if write_header:
        writer.writeheader()

    # Pre-create clients per model
    clients = {}
    for model_label in models:
        client, actual_name = get_client(model_label, args.ollama_url, args.vllm_url)
        clients[model_label] = (client, actual_name)

    # Run benchmark
    start_time = time.monotonic()
    errors = 0
    band_counts = {b: 0 for b in [BAND_EXACT, BAND_GHOST, BAND_PLAUSIBLE, BAND_SUSPICIOUS, BAND_OBVIOUS, BAND_PARSE_ERROR]}

    try:
        for i, (model_label, condition, inv_id) in enumerate(work, 1):
            client, actual_name = clients[model_label]
            gt = load_ground_truth(inv_id)
            manifest_row = manifest.get(inv_id, {})

            result = run_single(
                client=client,
                model_name=actual_name,
                model_label=model_label,
                invoice_id=inv_id,
                condition=condition,
                prompt_template=prompt_templates[condition],
                ground_truth=gt,
                manifest_row=manifest_row,
                dry_run=args.dry_run,
            )

            writer.writerow(result)
            csv_file.flush()

            # Track stats
            band = result.get("error_band", "")
            if band in band_counts:
                band_counts[band] += 1
            if band == BAND_PARSE_ERROR:
                errors += 1

            print_result_line(result, i, total_calls)

            if args.delay > 0 and i < total_calls:
                time.sleep(args.delay)

    except KeyboardInterrupt:
        print(f"\n\nInterrupted after {i} calls. Results saved to {output_path}")
        print("Re-run with --resume to continue.")
    finally:
        csv_file.close()

    elapsed_total = time.monotonic() - start_time

    # Summary
    print(f"\n{'=' * 50}")
    print(f"SUMMARY")
    print(f"{'=' * 50}")
    print(f"Total calls:    {total_calls}")
    print(f"Total time:     {elapsed_total:.1f}s ({elapsed_total/60:.1f}min)")
    print(f"Avg per call:   {elapsed_total/max(total_calls,1):.1f}s")
    print(f"Parse errors:   {errors}")
    print()
    print(f"Error band distribution:")
    for band, count in band_counts.items():
        pct = (count / max(total_calls, 1)) * 100
        bar = "#" * int(pct / 2)
        print(f"  {band:>12s}: {count:4d} ({pct:5.1f}%)  {bar}")
    print()
    print(f"Results saved to: {output_path}")


if __name__ == "__main__":
    main()