Python
fix(lab): retain relative artifact paths for grading (#27)
Rudy Mizrahi Celekli20d1f6192fd9
diff --git a/dependencies/ASSETS.json b/dependencies/ASSETS.jsonindex 17a7fd5..9eca93a 100644--- a/dependencies/ASSETS.json+++ b/dependencies/ASSETS.json@@ -27,7 +27,7 @@"third_party/licenses/job-bench-eval.LICENSE": "c71d239df91726fc519c6eb72d318ec65820627232b2f796219e87dcf35d0ab4","third_party/licenses/opencode.LICENSE": "625f0f619133f89bbbb2abe37369613dfa1885eba1e50d02170deb62bb42cb6b","third_party/licenses/workbuddy-bench.LICENSE": "b466fc7a63acc5cac4275fc4b0f1e22f332ad188ea600b217076ea3a8e040956",- "third_party/patches/harvey-labs-judge-vertex-anthropic.diff": "a6e8f7c5025f8a02ef5c654df2b3607ef7df5019d0d8b612938bc72f001a4848",+ "third_party/patches/harvey-labs-judge-vertex-anthropic.diff": "fc0c0d086264ac9227d9166dbfc5164f1ed69e3356dbf52a31b9704aaf5a1896","third_party/patches/job-bench-eval-judge.diff": "b5878bce91f6322c9fd1fa777b3a5dd3866922f704173a658d007c62456c5728","third_party/patches/opencode-compat.diff": "c9a8a0e826d48f3fcfc7d818321d128c28e1f7f7c594954c19453dfca4946dc8","third_party/patches/workbuddy-bench-runtime.diff": "1d581ee0c23a9a5574ce0f9317d0bda17f6c3a2556898c89b34ccbdbd5b87b3f",diff --git a/tests/test_lab_nested_deliverables.py b/tests/test_lab_nested_deliverables.pynew file mode 100644index 0000000..698bcd2--- /dev/null+++ b/tests/test_lab_nested_deliverables.py@@ -0,0 +1,212 @@+# Copyright 2026 Google LLC+# SPDX-License-Identifier: Apache-2.0+#+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# https://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.++"""Replay exact pinned LAB nested artifact loading through grading and cache consumers."""++import ast+import contextlib+import hashlib+import io+import json+import os+import re+import shutil+import subprocess+import tempfile+import unittest+from concurrent.futures import ThreadPoolExecutor+from dataclasses import asdict, dataclass, field+from datetime import datetime, timezone+from enum import StrEnum+from pathlib import Path+from types import SimpleNamespace+from unittest.mock import Mock++from harness2 import config+from harness2.benchmarks.lab import LabAdapter+from harness2.core.judge import judge_one++ROOT = Path(__file__).resolve().parents[1]+FIXTURE = ROOT / "tests/fixtures/harvey-labs"+PATCH = ROOT / "third_party/patches/harvey-labs-judge-vertex-anthropic.diff"+++def load_nodes(path, names, scope):+ """Execute exact upstream definitions, excluding dependency/client imports."""+ tree = ast.parse(path.read_text(encoding="utf-8"))+ nodes = []+ for node in tree.body:+ if isinstance(node, (ast.FunctionDef, ast.ClassDef)) and node.name in names:+ nodes.append(node)+ elif isinstance(node, ast.Assign) and any(+ isinstance(target, ast.Name) and target.id in names for target in node.targets+ ):+ nodes.append(node)+ exec(compile(ast.Module(body=nodes, type_ignores=[]), str(path), "exec"), scope)+++class LabNestedDeliverableTests(unittest.TestCase):+ def setUp(self):+ temporary = tempfile.TemporaryDirectory()+ self.addCleanup(temporary.cleanup)+ self.root = Path(temporary.name)+ provenance = json.loads((FIXTURE / "PROVENANCE.json").read_text())+ self.assertEqual(provenance["commit"], config.DEPENDENCY_PINS["harvey-labs"])+ for name, expected in provenance["sha256"].items():+ self.assertEqual(hashlib.sha256((FIXTURE / name).read_bytes()).hexdigest(), expected)+ assets = json.loads((ROOT / "dependencies/ASSETS.json").read_text())+ self.assertEqual(+ hashlib.sha256(PATCH.read_bytes()).hexdigest(),+ assets[PATCH.relative_to(ROOT).as_posix()],+ )+ upstream = self.root / "upstream"+ shutil.copytree(FIXTURE, upstream)+ for path in upstream.rglob("*.py.txt"):+ path.rename(path.with_suffix(""))+ subprocess.run(["git", "apply", str(PATCH)], cwd=upstream, check=True, capture_output=True)+ self.scope = {+ "__name__": __name__,+ "Path": Path,+ "json": json,+ "os": os,+ "re": re,+ "dataclass": dataclass,+ "field": field,+ "asdict": asdict,+ "StrEnum": StrEnum,+ "ThreadPoolExecutor": ThreadPoolExecutor,+ "subprocess": subprocess,+ "datetime": datetime,+ "timezone": timezone,+ "PROMPTS_DIR": upstream / "evaluation/prompts",+ "BENCH_ROOT": self.root,+ "RESULTS_DIR": self.root / "results",+ }+ load_nodes(upstream / "evaluation/judge.py", {"Judge"}, self.scope)+ load_nodes(+ upstream / "evaluation/scoring.py",+ {+ "DocxTrackChanges",+ "CriterionResult",+ "RubricResult",+ "_read_file_as_text",+ "_SKIP_DIRS",+ "_SKIP_EXTENSIONS",+ "_SKIP_FILES",+ "_load_all_output",+ "score_rubric",+ "_is_thread_export",+ "_fuzzy_match_filename",+ "_match_deliverables",+ },+ self.scope,+ )+ load_nodes(+ upstream / "evaluation/run_eval.py",+ {+ "REQUIRED_TASK_KEYS",+ "REQUIRED_CRITERION_KEYS",+ "validate_task_config",+ "_resolve_task_dir",+ "evaluate_run",+ },+ self.scope,+ )+ task = self.root / "tasks/contracts/task-1"+ task.mkdir(parents=True)+ (task / "task.json").write_text(+ json.dumps(+ {+ "title": "Review the local report",+ "instructions": "Write report.txt",+ "criteria": [+ {+ "id": "c1",+ "title": "Correct report",+ "match_criteria": "Review the report",+ }+ ],+ }+ )+ )+ self.run = self.root / "results/run-1"+ (self.run / "output").mkdir(parents=True)+ (self.run / "output/report.txt").write_text("The locally produced report.\n")++ def score(self, actual_files, expected_files):+ (self.run / "output/report.txt").unlink()+ for name, body in actual_files.items():+ path = self.run / "output" / name+ path.parent.mkdir(parents=True, exist_ok=True)+ path.write_text(body)+ task = self.root / "tasks/contracts/task-1/task.json"+ config = json.loads(task.read_text())+ if expected_files:+ config["criteria"][0]["deliverables"] = expected_files+ task.write_text(json.dumps(config))+ adapter = LabAdapter()+ self.assertTrue(adapter.delivered(self.run))++ def response(**kwargs):+ # A deterministic transport oracle checks whether actual artifact+ # bytes reached the judge, not the quality of professional work.+ prompt = kwargs["messages"][0]["content"]+ present = all(body in prompt for body in actual_files.values())+ text = json.dumps(+ {"verdict": "pass" if present else "fail", "reasoning": "local input oracle"}+ )+ return SimpleNamespace(+ content=[SimpleNamespace(type="text", text=text)], stop_reason="end_turn"+ )++ transport = Mock(side_effect=response)+ judge = object.__new__(self.scope["Judge"])+ judge.provider = "vertex-anthropic"+ judge.model = "fixture-judge"+ judge.client = SimpleNamespace(messages=SimpleNamespace(create=transport))+ with contextlib.redirect_stdout(io.StringIO()):+ result = self.scope["evaluate_run"]("run-1", "contracts/task-1", judge, parallel=1)+ self.assertTrue((self.run / "scores.json").is_file())+ cached = judge_one(adapter, "contracts/task-1", "agg", 1, "agg1", "run-1", self.run)+ self.assertEqual(cached.all_pass, result["all_pass"])+ observed = transport.call_args.kwargs["messages"][0]["content"]+ for body in actual_files.values():+ self.assertIn(body, observed)+ self.assertEqual(cached.frac, 1)++ def test_basename_request_reads_nested_artifact(self):+ self.score({"reports/report.txt": "The actual nested report.\n"}, ["report.txt"])++ def test_declared_relative_path_keeps_its_identity(self):+ self.score(+ {"reports/report.txt": "The explicitly requested nested report.\n"},+ ["reports/report.txt"],+ )++ def test_distinct_nested_files_with_the_same_basename(self):+ self.score(+ {"current/report.txt": "Current report.\n", "archive/report.txt": "Archived report.\n"},+ ["current/report.txt", "archive/report.txt"],+ )++ def test_root_file_control(self):+ self.score({"report.txt": "The ordinary root report.\n"}, ["report.txt"])++ def test_no_deliverable_list_control(self):+ self.score({"reports/report.txt": "The recursive fallback report.\n"}, [])+++if __name__ == "__main__":+ unittest.main()diff --git a/third_party/patches/harvey-labs-judge-vertex-anthropic.diff b/third_party/patches/harvey-labs-judge-vertex-anthropic.diffindex 95f2eec..47122e9 100644--- a/third_party/patches/harvey-labs-judge-vertex-anthropic.diff+++ b/third_party/patches/harvey-labs-judge-vertex-anthropic.diff@@ -232,3 +232,10 @@+ f"({sorted(found)}); refusing to guess: {text[:200]}")raise ValueError(f"No JSON found in judge response: {text[:200]}")+--- a/evaluation/scoring.py++++ b/evaluation/scoring.py+@@ -331,3 +331,3 @@+ if deliverables_map and output_dir.exists():+- actual_files = [f.name for f in output_dir.rglob("*") if f.is_file()]++ actual_files = [f.relative_to(output_dir).as_posix() for f in output_dir.rglob("*") if f.is_file()]+ resolved_map = _match_deliverables(deliverables_map, actual_files, output_dir=output_dir)