Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog.d/1273-example-cache-root.md
Original file line number Diff line number Diff line change
@@ -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).
7 changes: 2 additions & 5 deletions examples/adas/adas_meta_agent_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions examples/skill_evolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,19 @@
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 ---------------------------------------------------------


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:
Expand Down
31 changes: 31 additions & 0 deletions tests/test_adas_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
33 changes: 33 additions & 0 deletions tests/test_skill_evolution_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading