Datasets:
Download scripts/sanitize_dataset_paths.py from tesraghavan/agent-trace: direct link, hf CLI and curl.
- Browser
- Download file 5.2 kB
-
https://huggingface.co/datasets/tesraghavan/agent-trace/resolve/main/scripts/sanitize_dataset_paths.py
- Command line
-
hf download hf://datasets/tesraghavan/agent-trace/scripts/sanitize_dataset_paths.py
-
curl -L -o sanitize_dataset_paths.py https://huggingface.co/datasets/tesraghavan/agent-trace/resolve/main/scripts/sanitize_dataset_paths.py
5.2 kB
| """Rewrite host-specific testdata paths in stored traces to a stable alias.""" | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import re | |
| from pathlib import Path | |
| from typing import Any | |
| VIRTUAL_TESTDATA_ROOT = "/testdata" | |
| PRIVATE_PATH_PLACEHOLDER = "/private/path" | |
| ABSOLUTE_PATH_PATTERN = re.compile(r"/Users/[^\s\"'\\]+") | |
| ESCAPED_ABSOLUTE_PATH_PATTERN = re.compile(r"/Users(?:\\/[^\s\"']+)+") | |
| HOME_PATH_PATTERN = re.compile(r"/home/([A-Za-z0-9._-]+)") | |
| REDACTED_USER = "<redacted>" | |
| ENV_ASSIGNMENT_LINE_PATTERN = re.compile(r"(^|\n)([A-Z][A-Z0-9_]{1,})=([^\n]*)") | |
| def sanitize_text(text: str, absolute_testdata: str) -> tuple[str, int]: | |
| """Replace the absolute testdata prefix with the public virtual root.""" | |
| sanitized = text | |
| username = Path.home().name | |
| username_pattern = re.compile(rf"\b{re.escape(username)}\b") if username else None | |
| replacements = sanitized.count(absolute_testdata) | |
| if replacements: | |
| sanitized = sanitized.replace(absolute_testdata, VIRTUAL_TESTDATA_ROOT) | |
| escaped_testdata = absolute_testdata.replace("/", "\\/") | |
| escaped_replacements = sanitized.count(escaped_testdata) | |
| if escaped_replacements: | |
| sanitized = sanitized.replace(escaped_testdata, VIRTUAL_TESTDATA_ROOT.replace("/", "\\/")) | |
| replacements += escaped_replacements | |
| def replace_absolute_path(match: re.Match[str]) -> str: | |
| token = match.group(0) | |
| testdata_idx = token.find("/testdata") | |
| if testdata_idx >= 0: | |
| return VIRTUAL_TESTDATA_ROOT + token[testdata_idx + len("/testdata"):] | |
| return PRIVATE_PATH_PLACEHOLDER | |
| sanitized, generic_count = ABSOLUTE_PATH_PATTERN.subn(replace_absolute_path, sanitized) | |
| replacements += generic_count | |
| def replace_escaped_absolute_path(match: re.Match[str]) -> str: | |
| token = match.group(0).replace("\\/", "/") | |
| testdata_idx = token.find("/testdata") | |
| if testdata_idx >= 0: | |
| return VIRTUAL_TESTDATA_ROOT + token[testdata_idx + len("/testdata"):] | |
| return PRIVATE_PATH_PLACEHOLDER | |
| sanitized, escaped_generic_count = ESCAPED_ABSOLUTE_PATH_PATTERN.subn(replace_escaped_absolute_path, sanitized) | |
| replacements += escaped_generic_count | |
| sanitized, home_count = HOME_PATH_PATTERN.subn(f"/home/{REDACTED_USER}", sanitized) | |
| replacements += home_count | |
| if username_pattern is not None: | |
| sanitized, username_count = username_pattern.subn(REDACTED_USER, sanitized) | |
| replacements += username_count | |
| def redact_env_assignment(match: re.Match[str]) -> str: | |
| prefix, key, _value = match.groups() | |
| return f"{prefix}{key}=<redacted>" | |
| sanitized, env_count = ENV_ASSIGNMENT_LINE_PATTERN.subn(redact_env_assignment, sanitized) | |
| replacements += env_count | |
| return sanitized, replacements | |
| def sanitize_value(value: Any, absolute_testdata: str) -> tuple[Any, int]: | |
| """Recursively sanitize strings nested inside JSON-compatible data.""" | |
| if isinstance(value, dict): | |
| total = 0 | |
| sanitized = {} | |
| for key, nested in value.items(): | |
| sanitized[key], count = sanitize_value(nested, absolute_testdata) | |
| total += count | |
| return sanitized, total | |
| if isinstance(value, list): | |
| total = 0 | |
| sanitized = [] | |
| for nested in value: | |
| clean_nested, count = sanitize_value(nested, absolute_testdata) | |
| sanitized.append(clean_nested) | |
| total += count | |
| return sanitized, total | |
| if isinstance(value, str): | |
| return sanitize_text(value, absolute_testdata) | |
| return value, 0 | |
| def sanitize_file(path: Path, absolute_testdata: str) -> tuple[list[str], int]: | |
| """Return sanitized JSONL lines plus the number of replacements made.""" | |
| replacements = 0 | |
| sanitized_lines = [] | |
| with path.open() as f: | |
| for line in f: | |
| if not line.strip(): | |
| continue | |
| payload = json.loads(line) | |
| sanitized_payload, count = sanitize_value(payload, absolute_testdata) | |
| replacements += count | |
| sanitized_lines.append(json.dumps(sanitized_payload) + "\n") | |
| return sanitized_lines, replacements | |
| def main() -> None: | |
| parser = argparse.ArgumentParser(description="Sanitize absolute testdata paths in trace JSONLs") | |
| parser.add_argument("files", nargs="+", help="Trace JSONL files to sanitize") | |
| parser.add_argument( | |
| "--in-place", | |
| action="store_true", | |
| help="Overwrite the input files instead of writing <name>.sanitized.jsonl", | |
| ) | |
| args = parser.parse_args() | |
| repo_root = Path(__file__).resolve().parent.parent | |
| absolute_testdata = str((repo_root / "testdata").resolve()) | |
| for file_name in args.files: | |
| path = Path(file_name) | |
| sanitized_lines, replacements = sanitize_file(path, absolute_testdata) | |
| if args.in_place: | |
| output_path = path | |
| else: | |
| output_path = path.with_name(f"{path.stem}.sanitized{path.suffix}") | |
| with output_path.open("w") as f: | |
| f.writelines(sanitized_lines) | |
| print(f"{path} -> {output_path} ({replacements} replacements)") | |
| if __name__ == "__main__": | |
| main() | |