Skip to content
Python
14.5 KB
Raw Blame
"""Scoring functions for evaluating agent output against rubric criteria.

Each criterion is graded individually by an LLM judge, with only the
relevant deliverable files included in context.
"""

from __future__ import annotations

import json
import subprocess
from concurrent.futures import ThreadPoolExecutor
from enum import StrEnum

import anthropic
from dataclasses import dataclass, field, asdict
from pathlib import Path

import pandas as pd
import pdfplumber
from markitdown import MarkItDown


# ── File reading helpers ──────────────────────────────────────────────


class DocxTrackChanges(StrEnum):
    ACCEPT = "accept"
    ALL = "all"


def _read_file_as_text(path: Path, *, track_changes: DocxTrackChanges = DocxTrackChanges.ACCEPT) -> str:
    """Read a file and return its content as plain text.

    Uses the same extraction methods as the agent harness (harness/tools.py):
    pandoc for .docx, pandas for .xlsx, markitdown for .pptx, pdfplumber for .pdf.
    """
    suffix = path.suffix.lower()
    try:
        if suffix == ".docx":
            result = subprocess.run(
                ["pandoc", str(path), "-t", "markdown", "--wrap=none", f"--track-changes={track_changes.value}"],
                capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=30,
            )
            if result.returncode != 0:
                raise RuntimeError(f"pandoc failed: {result.stderr}")
            return result.stdout
        if suffix == ".xlsx":
            sheets = pd.read_excel(path, sheet_name=None)
            parts = []
            for sheet_name, df in sheets.items():
                parts.append(f"=== Sheet: {sheet_name} ===")
                parts.append(df.to_string(index=False))
            return "\n".join(parts)
        if suffix == ".pptx":
            md = MarkItDown()
            result = md.convert(str(path))
            return result.text_content
        if suffix == ".pdf":
            parts = []
            with pdfplumber.open(path) as pdf:
                for page in pdf.pages:
                    text = page.extract_text()
                    if text:
                        parts.append(text)
                    for table in page.extract_tables():
                        for row in table:
                            parts.append("\t".join(cell if cell else "" for cell in row))
                        parts.append("")
            return "\n".join(parts)
        return path.read_text(encoding="utf-8")
    except UnicodeDecodeError:
        return f"(binary file: {path.name})"
    except Exception as e:
        return f"(error reading {path.name}: {e})"


# ── Result dataclasses ────────────────────────────────────────────────

@dataclass
class CriterionResult:
    id: str
    title: str
    verdict: str  # "pass" or "fail"
    reasoning: str = ""

    def to_dict(self) -> dict:
        return asdict(self)

@dataclass
class RubricResult:
    score: float
    max_score: float
    criteria_results: list[dict] = field(default_factory=list)

    def to_dict(self) -> dict:
        return asdict(self)


# ── File matching ────────────────────────────────────────────────

def _is_thread_export(filename: str) -> bool:
    """Check if a file is the thread export (output.docx, output.md, etc.)."""
    return Path(filename).stem.lower() == "output"


def _fuzzy_match_filename(expected: str, candidates: list[str]) -> tuple[str | None, int]:
    """Find the best fuzzy match for an expected filename among candidates.

    Splits filenames into keywords (replacing hyphens and underscores with spaces)
    and returns the candidate with the highest keyword overlap.

    Args:
        expected: The expected filename (e.g., "case-chronology.xlsx").
        candidates: List of candidate filenames to match against.

    Returns:
        Tuple of (best matching filename or None, overlap score).
    """
    expected_stem = Path(expected).stem.lower().replace("-", " ").replace("_", " ")
    expected_words = set(expected_stem.split())

    best_match = None
    best_score = 0
    for candidate in candidates:
        candidate_stem = Path(candidate).stem.lower().replace("-", " ").replace("_", " ")
        candidate_words = set(candidate_stem.split())
        overlap = len(expected_words & candidate_words)
        if overlap > best_score:
            best_score = overlap
            best_match = candidate

    return best_match, best_score


def _match_deliverables(deliverables_map: dict, actual_files: list[str], output_dir: Path | None = None) -> dict:
    """Best-effort match expected deliverable filenames to actual output files.

    For each deliverable, if the expected filename exists exactly, use it.
    Otherwise, try to find the best match by:
    1. Matching by file extension (e.g., .xlsx → .xlsx)
    2. Fuzzy substring matching on the stem
    3. If only one file of the matching extension exists, use it
    4. LLM-based matching for any remaining unmatched deliverables

    Returns a new map with the same keys but resolved filenames.
    """
    resolved = {}
    used = set()

    for name, expected in deliverables_map.items():
        if expected in actual_files:
            resolved[name] = expected
            used.add(expected)
            continue

        expected_ext = Path(expected).suffix.lower()

        # Candidates with matching extension (exclude thread export)
        candidates = [
            f for f in actual_files
            if f not in used and not _is_thread_export(f) and Path(f).suffix.lower() == expected_ext
        ]

        if len(candidates) == 1:
            resolved[name] = candidates[0]
            used.add(candidates[0])
            print(f"  Matched deliverable '{name}': {expected} -> {candidates[0]} (only file with {expected_ext})")
            continue

        best_match, best_score = _fuzzy_match_filename(expected, candidates)

        if best_match:
            resolved[name] = best_match
            used.add(best_match)
            print(f"  Matched deliverable '{name}': {expected} -> {best_match} (fuzzy match, {best_score} words)")
        else:
            resolved[name] = expected
            print(f"  No fuzzy match for deliverable '{name}': {expected}")

    # LLM-based matching for any unresolved deliverables
    unresolved = {name: expected for name, expected in resolved.items()
                  if expected not in actual_files and expected == deliverables_map[name]}
    remaining_files = [f for f in actual_files if f not in used and not _is_thread_export(f)]

    if unresolved and remaining_files and output_dir:
        llm_matches = _llm_match_deliverables(unresolved, remaining_files, output_dir)
        for name, matched_file in llm_matches.items():
            if matched_file and matched_file in actual_files:
                resolved[name] = matched_file
                used.add(matched_file)
                print(f"  Matched deliverable '{name}': {deliverables_map[name]} -> {matched_file} (LLM match)")

    return resolved


def _llm_match_deliverables(
    unresolved: dict[str, str],
    available_files: list[str],
    output_dir: Path,
) -> dict[str, str | None]:
    """Use an LLM to match unresolved deliverables to available output files.

    Provides the model with deliverable names, expected filenames, available
    filenames, and a preview of each file's content.
    """
    # Build file previews
    file_previews = []
    for filename in available_files:
        filepath = output_dir / filename
        if filepath.exists():
            try:
                content = _read_file_as_text(filepath)[:500]
            except Exception:
                content = "(could not read file)"
        else:
            content = "(file not found)"
        file_previews.append(f"Filename: {filename}\nPreview: {content}\n")

    # Build deliverable descriptions
    deliverable_descriptions = []
    for name, expected in unresolved.items():
        deliverable_descriptions.append(f"Deliverable key: {name}\nExpected filename: {expected}")

    deliverables_text = "\n".join(deliverable_descriptions)
    files_text = "\n".join(file_previews)
    deliverable_keys = list(unresolved.keys())

    prompt = f"""Match each unresolved deliverable to the most likely output file.

## Unresolved Deliverables
{deliverables_text}

## Available Output Files
{files_text}

For each deliverable, provide the matching filename from the available files, or null if no file matches."""

    # Build JSON schema with the exact deliverable keys as properties
    schema_properties = {key: {"type": ["string", "null"]} for key in deliverable_keys}
    output_schema = {
        "type": "object",
        "properties": schema_properties,
        "required": deliverable_keys,
        "additionalProperties": False,
    }

    try:
        client = anthropic.Anthropic()
        response = client.messages.create(
            model="claude-sonnet-4-6",
            max_tokens=1024,
            temperature=0.0,
            messages=[{"role": "user", "content": prompt}],
            output_config={
                "format": {
                    "type": "json_schema",
                    "schema": output_schema,
                }
            },
        )
        return json.loads(response.content[0].text)
    except Exception as e:
        print(f"  LLM matching failed: {e}")

    return {}


# ── Rubric Scoring ───────────────────────────────────────────────

# Directories and extensions to skip when loading all output (build artifacts)
_SKIP_DIRS = {"node_modules", ".npm", "__pycache__", ".git", "venv", ".venv"}
_SKIP_EXTENSIONS = {".lock", ".map"}
_SKIP_FILES = {"package-lock.json"}


def _load_all_output(output_dir: Path) -> str:
    """Read all files in the output directory as a single text block.

    Skips build artifacts (node_modules, lockfiles, etc.) to avoid
    blowing up the judge context window.
    """
    sections = []
    if output_dir.exists():
        for f in sorted(output_dir.rglob("*")):
            if not f.is_file():
                continue
            # Skip build artifact directories
            if any(part in _SKIP_DIRS for part in f.relative_to(output_dir).parts):
                continue
            # Skip lockfiles and sourcemaps
            if f.suffix in _SKIP_EXTENSIONS or f.name in _SKIP_FILES:
                continue
            content = _read_file_as_text(f)
            sections.append(f"## {f.relative_to(output_dir)}\n{content}")
    return "\n\n".join(sections) if sections else "(No agent output found)"


def score_rubric(
    criteria: list[dict],
    run_dir,
    judge,
    task_desc: str,
    parallel: int,
) -> RubricResult:
    """Score agent output against rubric criteria with deliverable-aware file loading.

    Each criterion declares which output files (deliverables) are relevant to it
    via its 'deliverables' list. Only those files are loaded into context for
    the judge. Criteria without a 'deliverables' list fall back to loading all
    output files.

    Args:
        criteria: List of criterion dicts from task.json.
        run_dir: Path to the run directory (contains output/ folder).
        judge: Judge instance for LLM evaluation.
        task_desc: Task title for context in the judge prompt.
        parallel: Number of judge calls to run concurrently.
    """
    run_dir = Path(run_dir)
    output_dir = run_dir / "output"

    # Build deliverable map from criterion-level deliverables lists.
    # Each criterion lists expected output filenames directly (e.g., "nda-term-sheet.docx").
    filenames = set()
    for c in criteria:
        for d in c.get("deliverables", []):
            filenames.add(d)
    deliverables_map = {f: f for f in filenames} if filenames else None

    # Match expected deliverable filenames to actual output files
    if deliverables_map and output_dir.exists():
        actual_files = [f.name for f in output_dir.rglob("*") if f.is_file()]
        resolved_map = _match_deliverables(deliverables_map, actual_files, output_dir=output_dir)
    else:
        resolved_map = None

    # Pre-load full output for tasks without per-criterion deliverables
    full_output = None
    if any(not (c.get("deliverables") and resolved_map) for c in criteria):
        full_output = _load_all_output(output_dir)

    def _score_one(criterion: dict) -> CriterionResult:
        criterion_deliverables = criterion.get("deliverables", [])
        if criterion_deliverables and resolved_map:
            sections = []
            for name in criterion_deliverables:
                filename = resolved_map[name]
                filepath = output_dir / filename
                if not filepath.exists():
                    sections.append(f"## Agent Output: {name}\n(File not found: {filename})")
                    continue
                include_redlines = criterion.get("evaluation_options", {}).get("include_docx_redlines", False)
                track_changes = DocxTrackChanges.ALL if include_redlines else DocxTrackChanges.ACCEPT
                content = _read_file_as_text(filepath, track_changes=track_changes)
                sections.append(f"## Agent Output: {name}\n{content}")
            agent_output = "\n\n".join(sections) if sections else "(No agent output found)"
        else:
            agent_output = full_output

        result = judge.evaluate_from_file(
            prompt_name="rubric_criterion",
            variables={
                "task_description": task_desc,
                "agent_output": agent_output,
                "criterion_title": criterion["title"],
                "match_criteria": criterion["match_criteria"],
            },
        )

        verdict = result.get("verdict", "fail").lower()
        reasoning = result.get("reasoning", "")

        return CriterionResult(
            id=criterion["id"],
            title=criterion["title"],
            verdict=verdict,
            reasoning=reasoning,
        )

    with ThreadPoolExecutor(max_workers=max(parallel, 1)) as pool:
        criteria_results = list(pool.map(_score_one, criteria))

    # All-pass grading: task scores 1.0 only if every criterion passed.
    n_total = len(criteria_results)
    n_passed = sum(1 for c in criteria_results if c.verdict == "pass")
    score = 1.0 if n_total > 0 and n_passed == n_total else 0.0

    return RubricResult(
        score=score,
        max_score=1.0,
        criteria_results=[c.to_dict() for c in criteria_results],
    )