Codex
Replace the dataset with the current testing tree and the three UT docs.
d26f22a
Raw History Blame Contribute Delete
5.02 kB
"""Write the retained local audit bundle for one completed experiment."""
from __future__ import annotations
import json
import shutil
from pathlib import Path
from typing import Any, TYPE_CHECKING
from .models import write_jsonl
from .reporting.render import render_evaluation_document
from .reporting.models import ReportData
from urllib.parse import quote
if TYPE_CHECKING:
from .loop import EvaluationResult
def _write_json(path: Path, value: Any) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(value, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
class ExperimentArtifactWriter:
"""Archive raw evidence and write one compact, self-indexing artifact tree."""
@classmethod
def write(cls, result: EvaluationResult, artifact_root: str | Path) -> Path:
root = Path(artifact_root) / result.experiment_id
root.mkdir(parents=True, exist_ok=True)
_write_json(root / "testpoints.json", result.testpoint_set.to_dict())
run_entries: list[dict[str, Any]] = []
for run in result.runs:
relative_run_root = Path(run.case_id) / run.run_id
run_root = root / relative_run_root
(run_root / "ai-output").mkdir(parents=True, exist_ok=True)
(run_root / "raw").mkdir(exist_ok=True)
archived_refs, unarchived_refs = cls._archive_run_files(run, root, run_root)
artifact_data = run.to_dict()
artifact_data["artifact_refs"] = archived_refs
metadata = dict(artifact_data.get("metadata") or {})
if unarchived_refs:
metadata["unarchived_artifact_refs"] = unarchived_refs
artifact_data["metadata"] = metadata
_write_json(run_root / "task-artifact.json", artifact_data)
run_entries.append({
"case_id": run.case_id,
"run_id": run.run_id,
"task_artifact": (relative_run_root / "task-artifact.json").as_posix(),
"ai_output": (relative_run_root / "ai-output").as_posix(),
"raw": (relative_run_root / "raw").as_posix(),
})
write_jsonl(root / "scores.jsonl", result.scores)
write_jsonl(root / "feedback.jsonl", result.feedback)
candidate_path: str | None = None
if result.candidate:
candidate_path = "candidate-skill.json"
_write_json(root / candidate_path, result.candidate.to_dict())
report = ReportData(
experiment_id=result.experiment_id,
runs=tuple(result.runs), scores=tuple(result.scores),
inputs={"skill": result.skill.ref_dict(),
"evaluation_profile": result.evaluation_profile.reference_dict(),
"dataset": result.dataset.manifest.to_dict()},
backend={"name": result.backend_name, "experiment_id": result.experiment_id,
"metadata": result.backend_metadata},
)
document = render_evaluation_document(
report, backend_ui_url=result.backend_metadata.get("ui_url"),
task_links={entry["run_id"]: "../" + quote(entry["task_artifact"], safe="/")
for entry in run_entries},
)
report_path = root / "report" / "index.html"
report_path.parent.mkdir(parents=True, exist_ok=True)
report_path.write_text(document, encoding="utf-8")
artifacts = {
"testpoints": "testpoints.json",
"scores": "scores.jsonl",
"feedback": "feedback.jsonl",
"candidate": candidate_path,
"report": report_path.relative_to(root).as_posix(),
"runs": run_entries,
}
_write_json(root / "summary.json", result.summary(artifacts=artifacts))
return root
@staticmethod
def _archive_run_files(run: Any, experiment_root: Path, run_root: Path) -> tuple[list[str], list[str]]:
archived: list[str] = []
unarchived: list[str] = []
workspace_value = str((run.metadata or {}).get("task_workspace", ""))
workspace = Path(workspace_value).resolve() if workspace_value else None
for ref in run.artifact_refs:
source = Path(ref)
if not source.is_file():
unarchived.append(str(ref))
continue
relative: Path | None = None
if workspace is not None:
try:
relative = source.resolve().relative_to(workspace)
except ValueError:
pass
if relative is not None and relative.parts and relative.parts[0] == "ai-output":
target = run_root / relative
else:
target = run_root / "raw" / source.name
target.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(source, target)
archived.append(target.relative_to(experiment_root).as_posix())
return archived, unarchived