diff --git a/changelog.d/1273-example-cache-root.md b/changelog.d/1273-example-cache-root.md new file mode 100644 index 00000000..74b20d50 --- /dev/null +++ b/changelog.d/1273-example-cache-root.md @@ -0,0 +1,5 @@ +Fixed +`examples.skill_evolution.download_bbh` and the ADAS hard-subset cache now +write through `dataloader.cache_path`, so they honor `AGENTDESCENT_CACHE` / +`XDG_CACHE_HOME` instead of always mkdir'ing under `~/.cache/agentdescent` +(#1273). diff --git a/examples/adas/adas_meta_agent_search.py b/examples/adas/adas_meta_agent_search.py index 89a48cce..903ea36a 100644 --- a/examples/adas/adas_meta_agent_search.py +++ b/examples/adas/adas_meta_agent_search.py @@ -55,7 +55,7 @@ from agentdescent.agents import Usage from agentdescent.aggregator import AggregatorProtocol, MergeOutcome, MergeReport -from agentdescent.dataloader import Dataset, fetch_text, split_dataset +from agentdescent.dataloader import Dataset, cache_path, fetch_text, split_dataset from agentdescent.evolvable import Diff, EvidenceCard from agentdescent.evolution import EvolvingArtifact, Task, evolve, rule_id from agentdescent.governance import classify @@ -746,16 +746,13 @@ class _HardCache: def __init__(self, model: str, enabled: bool = True) -> None: import hashlib - import os self.enabled = enabled key = hashlib.sha256(model.encode()).hexdigest()[:12] - root = os.path.join(os.path.expanduser("~/.cache/agentdescent"), "mgsm", "hard") - self.path = os.path.join(root, f"direct_{key}.json") + self.path = cache_path("mgsm/hard", f"direct_{key}.json") if enabled else "" self._d = {} self._dirty = False self._lock = threading.Lock() if enabled: - os.makedirs(root, exist_ok=True) try: with open(self.path, encoding="utf-8") as fh: self._d = json.load(fh) diff --git a/examples/skill_evolution.py b/examples/skill_evolution.py index c279780a..5b9e2f49 100644 --- a/examples/skill_evolution.py +++ b/examples/skill_evolution.py @@ -35,11 +35,11 @@ from typing import Dict, List, Optional from agentdescent.agents import Usage, claude, openai_compatible +from agentdescent.dataloader import cache_path from agentdescent.evolution import AppendRules, LLMAgent, Task, evolve from agentdescent.parallel import DataParallel BBH_URL = "https://raw.githubusercontent.com/suzgunmirac/BIG-Bench-Hard/main/bbh/{task}.json" -CACHE_DIR = os.path.expanduser("~/.cache/agentdescent/bbh") # -- dataset loading --------------------------------------------------------- @@ -47,8 +47,7 @@ def download_bbh(task: str) -> List[dict]: """Fetch a BBH task's examples (cached locally after first download).""" - os.makedirs(CACHE_DIR, exist_ok=True) - path = os.path.join(CACHE_DIR, f"{task}.json") + path = cache_path("bbh", f"{task}.json") if not os.path.exists(path): url = BBH_URL.format(task=task) with urllib.request.urlopen(url, timeout=30) as resp: diff --git a/tests/test_adas_example.py b/tests/test_adas_example.py index b664ca79..cebc6963 100644 --- a/tests/test_adas_example.py +++ b/tests/test_adas_example.py @@ -340,3 +340,34 @@ def test_a_missing_score_prints_as_missing(): assert fmt_score(0.4375).strip() == "0.438" assert fmt_score(None).strip() == "n/a" assert len(fmt_score(None)) == len(fmt_score(0.5)) # column stays aligned + + +def test_hard_cache_uses_dataset_cache_root(tmp_path, monkeypatch): + """#1273: the ADAS hard-subset cache follows AGENTDESCENT_CACHE.""" + import hashlib + + from examples.adas.adas_meta_agent_search import _HardCache + + root = tmp_path / "cache" + monkeypatch.setenv("AGENTDESCENT_CACHE", str(root)) + monkeypatch.delenv("XDG_CACHE_HOME", raising=False) + + model = "dummy-model" + cache = _HardCache(model, enabled=True) + key = hashlib.sha256(model.encode()).hexdigest()[:12] + expected = root / "mgsm" / "hard" / f"direct_{key}.json" + assert cache.path == str(expected) + assert expected.parent.is_dir() + + +def test_hard_cache_disabled_does_not_create_a_directory(tmp_path, monkeypatch): + """#1273: a disabled hard cache must not mkdir, matching today's behaviour.""" + from examples.adas.adas_meta_agent_search import _HardCache + + root = tmp_path / "cache" + monkeypatch.setenv("AGENTDESCENT_CACHE", str(root)) + monkeypatch.delenv("XDG_CACHE_HOME", raising=False) + + cache = _HardCache("dummy-model", enabled=False) + assert cache.path == "" + assert not (root / "mgsm").exists() diff --git a/tests/test_skill_evolution_example.py b/tests/test_skill_evolution_example.py index 4bd1ab58..f62902df 100644 --- a/tests/test_skill_evolution_example.py +++ b/tests/test_skill_evolution_example.py @@ -62,3 +62,36 @@ def test_build_tasks_shapes_examples(): def test_estimate_calls_scales(): assert estimate_calls(4, 2, 10) > estimate_calls(2, 2, 10) assert estimate_calls(4, 4, 10) > estimate_calls(4, 2, 10) + + +def test_download_bbh_uses_dataset_cache_root(tmp_path, monkeypatch): + """#1273: BBH cache follows AGENTDESCENT_CACHE instead of ~/.cache.""" + import json + import urllib.request + + from examples.skill_evolution import download_bbh + + root = tmp_path / "cache" + monkeypatch.setenv("AGENTDESCENT_CACHE", str(root)) + monkeypatch.delenv("XDG_CACHE_HOME", raising=False) + + payload = json.dumps({"examples": [{"input": "q", "target": "a"}]}).encode() + + class Resp: + def __enter__(self): + return self + + def __exit__(self, *a): + return False + + def read(self): + return payload + + monkeypatch.setattr(urllib.request, "urlopen", lambda *a, **k: Resp()) + + rows = download_bbh("hyperbaton") + + assert rows == [{"input": "q", "target": "a"}] + expected = root / "bbh" / "hyperbaton.json" + assert expected.is_file() + assert json.loads(expected.read_text())["examples"] == rows