|
| 1 | +"""Per-sample report and artifact folders next to the active sample's eval log. |
| 2 | +
|
| 3 | +METR evals write two kinds of per-sample output next to the eval log: |
| 4 | +
|
| 5 | +- ``reports/{sample_uuid}/`` — one report per sample (possibly several files |
| 6 | + that together form it). |
| 7 | +- ``artifacts/{sample_uuid}/`` — many files, potentially accumulated over a run. |
| 8 | +
|
| 9 | +Uses ``UPath`` so the destination can be a local path or an ``s3://...`` URL |
| 10 | +without separate code paths. |
| 11 | +""" |
| 12 | + |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +from collections.abc import Mapping |
| 16 | +from posixpath import basename |
| 17 | + |
| 18 | +from inspect_ai.log._samples import sample_active # noqa: PLC2701 |
| 19 | +from upath import UPath |
| 20 | + |
| 21 | + |
| 22 | +def _validate_flat_path_component(component: str) -> None: |
| 23 | + if ( |
| 24 | + not component |
| 25 | + or component in {".", ".."} |
| 26 | + or basename(component) != component |
| 27 | + or "\\" in component |
| 28 | + or ":" in component |
| 29 | + or any(ord(c) < 32 or ord(c) == 0x7F for c in component) |
| 30 | + ): |
| 31 | + raise ValueError(f"invalid path component: {component!r}") |
| 32 | + |
| 33 | + |
| 34 | +def _sample_dir(subdir: str, sample_uuid: str) -> UPath | None: |
| 35 | + """Resolve ``{eval_log_folder}/{subdir}/{sample_uuid}`` for the active sample. |
| 36 | +
|
| 37 | + Returns ``None`` when there is no active sample (e.g. running outside an |
| 38 | + Inspect AI evaluation). Does not create the directory. |
| 39 | + """ |
| 40 | + active = sample_active() |
| 41 | + if active is None: |
| 42 | + return None |
| 43 | + |
| 44 | + _validate_flat_path_component(subdir) |
| 45 | + _validate_flat_path_component(sample_uuid) |
| 46 | + |
| 47 | + log_path = UPath(active.log_location) |
| 48 | + return log_path.parent / subdir / sample_uuid |
| 49 | + |
| 50 | + |
| 51 | +def report_dir(sample_uuid: str) -> UPath | None: |
| 52 | + """Return the sample's report directory (``reports/{sample_uuid}/``). |
| 53 | +
|
| 54 | + Returns ``None`` when there is no active sample. Does not create the |
| 55 | + directory. |
| 56 | + """ |
| 57 | + return _sample_dir("reports", sample_uuid) |
| 58 | + |
| 59 | + |
| 60 | +def artifacts_dir(sample_uuid: str) -> UPath | None: |
| 61 | + """Return the sample's artifacts directory (``artifacts/{sample_uuid}/``). |
| 62 | +
|
| 63 | + Returns ``None`` when there is no active sample. Does not create the |
| 64 | + directory. |
| 65 | + """ |
| 66 | + return _sample_dir("artifacts", sample_uuid) |
| 67 | + |
| 68 | + |
| 69 | +def _write_files(dest: UPath, files: Mapping[str, bytes | str], *, clear: bool) -> None: |
| 70 | + """Write ``files`` into ``dest``, validating each name. |
| 71 | +
|
| 72 | + When ``clear`` is true, removes any pre-existing contents of ``dest`` first. |
| 73 | + """ |
| 74 | + for name in files: |
| 75 | + _validate_flat_path_component(name) |
| 76 | + |
| 77 | + if dest.is_dir() and not dest.is_symlink(): |
| 78 | + if clear: |
| 79 | + for old in dest.iterdir(): |
| 80 | + if old.is_dir() and not old.is_symlink(): |
| 81 | + old.rmdir(recursive=True) |
| 82 | + else: |
| 83 | + old.unlink(missing_ok=True) |
| 84 | + elif dest.is_symlink() or dest.exists(): |
| 85 | + # a file or symlink sits where the directory should be; remove it |
| 86 | + dest.unlink(missing_ok=True) |
| 87 | + |
| 88 | + dest.mkdir(parents=True, exist_ok=True) |
| 89 | + |
| 90 | + for name, content in files.items(): |
| 91 | + target = dest / name |
| 92 | + if isinstance(content, bytes): |
| 93 | + target.write_bytes(content) |
| 94 | + else: |
| 95 | + target.write_text(content, encoding="utf-8") |
| 96 | + |
| 97 | + |
| 98 | +def write_report(sample_uuid: str, files: Mapping[str, bytes | str]) -> str | None: |
| 99 | + """Write the sample's report to ``reports/{sample_uuid}/``. |
| 100 | +
|
| 101 | + Replaces the whole report directory (the report is regenerated as a unit). |
| 102 | + Returns the destination directory path as a string, or ``None`` when there |
| 103 | + is no active sample. |
| 104 | + """ |
| 105 | + dest = report_dir(sample_uuid) |
| 106 | + if dest is None: |
| 107 | + return None |
| 108 | + _write_files(dest, files, clear=True) |
| 109 | + return str(dest) |
| 110 | + |
| 111 | + |
| 112 | +def write_artifacts( |
| 113 | + sample_uuid: str, |
| 114 | + files: Mapping[str, bytes | str], |
| 115 | + clear: bool = False, |
| 116 | +) -> str | None: |
| 117 | + """Write artifact ``files`` to ``artifacts/{sample_uuid}/``. |
| 118 | +
|
| 119 | + Additive by default: writes/overwrites only the named files, leaving other |
| 120 | + existing artifacts in place. Pass ``clear=True`` to wipe the directory |
| 121 | + first. Returns the destination directory path as a string, or ``None`` when |
| 122 | + there is no active sample. In additive mode, writing a file whose name |
| 123 | + collides with an existing subdirectory raises an error. |
| 124 | + """ |
| 125 | + dest = artifacts_dir(sample_uuid) |
| 126 | + if dest is None: |
| 127 | + return None |
| 128 | + _write_files(dest, files, clear=clear) |
| 129 | + return str(dest) |
| 130 | + |
| 131 | + |
| 132 | +def write_artifact(sample_uuid: str, name: str, content: bytes | str) -> str | None: |
| 133 | + """Write a single artifact file to ``artifacts/{sample_uuid}/{name}``. |
| 134 | +
|
| 135 | + Additive: never clears the directory. Returns the written file path as a |
| 136 | + string, or ``None`` when there is no active sample. Writing a file whose |
| 137 | + name collides with an existing subdirectory raises an error. |
| 138 | + """ |
| 139 | + dest = artifacts_dir(sample_uuid) |
| 140 | + if dest is None: |
| 141 | + return None |
| 142 | + _write_files(dest, {name: content}, clear=False) |
| 143 | + return str(dest / name) |
0 commit comments