diff --git a/agentdescent/dataloader.py b/agentdescent/dataloader.py index fc1ad4e8..7ffa36c9 100644 --- a/agentdescent/dataloader.py +++ b/agentdescent/dataloader.py @@ -196,19 +196,25 @@ def page_offsets(limit: int, page: int = PAGE_MAX) -> List[Tuple[int, int]]: def _cache_root() -> str: - """The dataset cache root, resolved at call time (#1261). + """The dataset cache root, resolved at call time (#1261, #1280). A host with a read-only home cannot create anything under ``~/.cache``, so the two standard knobs move the root: ``AGENTDESCENT_CACHE`` names it directly and ``XDG_CACHE_HOME`` puts it under ``/agentdescent``. With neither set the root is :data:`CACHE_ROOT`, exactly as before. + + A moved root is made absolute once, here (#1280), with the normalisation + :func:`runstore._normalise` already applies to the run store (#402/#403): + a run is written by one process and executed by another, and the detached + child starts in the run directory, so a relative knob must not be handed + back bare. """ explicit = os.environ.get("AGENTDESCENT_CACHE") if explicit: - return os.path.expanduser(explicit) + return os.path.abspath(os.path.expanduser(explicit)) xdg = os.environ.get("XDG_CACHE_HOME") if xdg: - return os.path.join(os.path.expanduser(xdg), "agentdescent") + return os.path.join(os.path.abspath(os.path.expanduser(xdg)), "agentdescent") return CACHE_ROOT @@ -413,10 +419,12 @@ def hf_rows_modelscope(url: str = MODELSCOPE_HOTPOTQA_URL, *, dict the examples read. With ``cache=True`` (default), responses live under - ``~/.cache/agentdescent/modelscope/`` keyed by the URL so a repeat call - does not re-download the ~46 MB file. With ``cache=False``, any existing - cache entry is ignored and the fresh response is not written back — the - same bypass contract as :func:`hf_rows`. + ``/modelscope/`` keyed by the URL so a repeat call does not + re-download the ~46 MB file. The root is :func:`_cache_root`: + ``~/.cache/agentdescent`` unless ``AGENTDESCENT_CACHE`` names it or + ``XDG_CACHE_HOME`` moves it, and absolute either way. With ``cache=False``, + any existing cache entry is ignored and the fresh response is not written + back — the same bypass contract as :func:`hf_rows`. """ rows = _cached_json(_raw_cache_path(url, "modelscope", None), url, cache=cache, timeout=timeout) diff --git a/agentdescent/runstore.py b/agentdescent/runstore.py index ea4262f0..cd494049 100644 --- a/agentdescent/runstore.py +++ b/agentdescent/runstore.py @@ -921,6 +921,18 @@ def launch(rd: RunDir, *, budget_usd: Optional[float] = None, # prefix, so it survives either way. child_env = scrub_host_env({**os.environ, "PYTHONPATH": _child_pythonpath(), **(env or {})}) + # The dataset cache root is resolved against the cwd of whichever process + # reads it, and the child reads it at ``cwd=rd.path`` -- so an operator's + # ``AGENTDESCENT_CACHE=cache`` used to name one tree in the CLI and another + # in the run it started, moving the cache with every run directory (#1280). + # Only this launch still knows the cwd the value was typed in, so a relative + # value is spelled absolute here, after the scrub and after the caller's + # ``env`` is merged; an absolute value is forwarded untouched. Datasets only + # (#402/#403 normalised the store the same way). + for knob in ("AGENTDESCENT_CACHE", "XDG_CACHE_HOME"): + wanted = child_env.get(knob) + if wanted and not os.path.isabs(os.path.expanduser(wanted)): + child_env[knob] = os.path.abspath(os.path.expanduser(wanted)) # The Ticket's handoff markers are inherited, not scoped: a process that was # itself launched below its ceiling would hand that number to every later # child, and a caller who legitimately asks for exactly that number would be diff --git a/changelog.d/1280-cache-root-abs.md b/changelog.d/1280-cache-root-abs.md new file mode 100644 index 00000000..d487ffa5 --- /dev/null +++ b/changelog.d/1280-cache-root-abs.md @@ -0,0 +1,8 @@ +Fixed +`AGENTDESCENT_CACHE` and `XDG_CACHE_HOME` are now read back as absolute paths, +the same half `#403` gave the run store: `cache_path()` no longer returns (or +creates) a bare relative directory. `hf_rows_modelscope`'s docstring names both +knobs instead of one fixed `~/.cache/agentdescent/modelscope/`, and +`runstore.launch` resolves both knobs against the launcher's cwd before the +detached child starts in the run directory, so a relative value names one tree +for the run and for the process that started it (#1280). diff --git a/tests/test_dataloader_cache_root_abs.py b/tests/test_dataloader_cache_root_abs.py new file mode 100644 index 00000000..d38b2132 --- /dev/null +++ b/tests/test_dataloader_cache_root_abs.py @@ -0,0 +1,85 @@ +"""The dataset cache root is absolute, so the run's cache does not move with a cwd (#1280). + +A run is written by one process and executed by another: the detached child +starts in the run directory (``runstore`` launches it with ``cwd=rd.path``), so +a relative ``AGENTDESCENT_CACHE`` or ``XDG_CACHE_HOME`` names a different tree +in each process. ``#402``/``#403`` already normalised the store base with +``abspath(expanduser(...))`` for exactly this reason; this is the dataset cache +catching up, with the default ``CACHE_ROOT`` left byte-identical. +""" + +import inspect +import os + +from agentdescent import dataloader as dl + + +def test_relative_agentdescent_cache_resolves_absolute(tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + monkeypatch.setenv("AGENTDESCENT_CACHE", "rel-cache") + monkeypatch.delenv("XDG_CACHE_HOME", raising=False) + + root = dl._cache_root() + + assert os.path.isabs(root), root + assert root == str(tmp_path / "rel-cache") + path = dl.cache_path("gsm8k", "x.jsonl") + assert os.path.isabs(path), path + assert path == str(tmp_path / "rel-cache" / "gsm8k" / "x.jsonl") + + +def test_relative_xdg_cache_home_resolves_absolute(tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + monkeypatch.delenv("AGENTDESCENT_CACHE", raising=False) + monkeypatch.setenv("XDG_CACHE_HOME", "rel-xdg") + + root = dl._cache_root() + + assert os.path.isabs(root), root + assert root == str(tmp_path / "rel-xdg" / "agentdescent") + path = dl.cache_path("gsm8k", "x.jsonl") + assert os.path.isabs(path), path + assert path == str(tmp_path / "rel-xdg" / "agentdescent" / "gsm8k" / "x.jsonl") + + +def test_relative_root_is_absolute_whatever_the_cwd(tmp_path, monkeypatch): + """A bare relative value is absolute wherever a process reads it. + + This is the resolution rule itself, so it is the cwd of the reader that + applies (the same contract ``#403`` gave the run store) and + ``cache_path`` never hands back a bare relative path. A run is not exposed + to that: ``runstore.launch`` spells the knob absolute before the child + starts in the run directory, so the run and its launcher name one tree + (``test_runstore_cache_env.py``). + """ + run_dir = tmp_path / "run" + run_dir.mkdir() + monkeypatch.setenv("AGENTDESCENT_CACHE", "rel-cache") + monkeypatch.delenv("XDG_CACHE_HOME", raising=False) + + monkeypatch.chdir(tmp_path) # the process that starts the run + started_from = dl._cache_root() + assert os.path.isabs(started_from) + assert started_from == str(tmp_path / "rel-cache") + + monkeypatch.chdir(run_dir) # the detached child's cwd + in_child = dl._cache_root() + assert os.path.isabs(in_child) + assert in_child == str(run_dir / "rel-cache") + assert os.path.isabs(dl.cache_path("gsm8k", "x.jsonl")) + + +def test_without_the_variables_the_root_is_unchanged(tmp_path, monkeypatch): + monkeypatch.delenv("AGENTDESCENT_CACHE", raising=False) + monkeypatch.delenv("XDG_CACHE_HOME", raising=False) + monkeypatch.setattr(dl, "CACHE_ROOT", str(tmp_path / "default")) + + assert dl._cache_root() == str(tmp_path / "default") + + +def test_modelscope_docstring_names_the_knobs(): + doc = inspect.getdoc(dl.hf_rows_modelscope) or "" + + assert "AGENTDESCENT_CACHE" in doc + assert "XDG_CACHE_HOME" in doc + assert "~/.cache/agentdescent/modelscope/" not in doc diff --git a/tests/test_runstore_cache_env.py b/tests/test_runstore_cache_env.py new file mode 100644 index 00000000..659490bd --- /dev/null +++ b/tests/test_runstore_cache_env.py @@ -0,0 +1,90 @@ +"""The detached child inherits the launcher's cache root, so both name one tree (#1280). + +``runstore.launch`` scrubs the environment but deliberately keeps +``AGENTDESCENT_*`` and starts the child with ``cwd=rd.path``. A relative +``AGENTDESCENT_CACHE`` -- or ``XDG_CACHE_HOME`` -- therefore names one tree in +the process that launched the run and another in the child: the dataset cache +moves with every run directory. Only the launcher still knows the cwd the +operator typed the value in, so it resolves both knobs before the value crosses +the detach boundary. Nothing here starts a real process. +""" + +import os +import subprocess + +import pytest + +from agentdescent import dataloader as dl +from agentdescent import runstore + +from tests.test_evolvespec import _dir_spec + + +@pytest.fixture +def store(tmp_path, monkeypatch): + monkeypatch.setenv("AGENTDESCENT_HOME", str(tmp_path / "home")) + return str(tmp_path / "runs") + + +def _launched_child_env(tmp_path, monkeypatch): + """Run ``launch`` with a fake detach; return the env, cwd and run dir it used.""" + rd = runstore.create(_dir_spec(str(tmp_path)).to_dict(), + store=str(tmp_path / "runs")) + calls = [] + + def fake_run(argv, **kw): + calls.append((argv, kw)) + # A pid that cannot be alive on this machine, so no status here lies. + return subprocess.CompletedProcess(argv, 0, stdout="2000000000\n", + stderr="") + + monkeypatch.setattr(runstore.subprocess, "run", fake_run) + runstore.launch(rd) + + assert len(calls) == 1, calls + _, kw = calls[0] + assert kw["cwd"] == rd.path, "the child starts in the run directory" + return kw["env"], rd + + +def test_relative_agentdescent_cache_reaches_the_child_absolute(store, tmp_path, + monkeypatch): + monkeypatch.chdir(tmp_path) + monkeypatch.setenv("AGENTDESCENT_CACHE", "rel-cache") + monkeypatch.delenv("XDG_CACHE_HOME", raising=False) + + env, rd = _launched_child_env(tmp_path, monkeypatch) + + value = env["AGENTDESCENT_CACHE"] + assert os.path.isabs(value), value + assert value == str(tmp_path / "rel-cache") + # Read where the child reads it: still the tree the operator meant. + monkeypatch.chdir(rd.path) + monkeypatch.setenv("AGENTDESCENT_CACHE", value) + assert dl._cache_root() == str(tmp_path / "rel-cache") + + +def test_relative_xdg_cache_home_reaches_the_child_absolute(store, tmp_path, + monkeypatch): + monkeypatch.chdir(tmp_path) + monkeypatch.delenv("AGENTDESCENT_CACHE", raising=False) + monkeypatch.setenv("XDG_CACHE_HOME", "rel-xdg") + + env, rd = _launched_child_env(tmp_path, monkeypatch) + + value = env["XDG_CACHE_HOME"] + assert os.path.isabs(value), value + assert value == str(tmp_path / "rel-xdg") + monkeypatch.chdir(rd.path) + monkeypatch.setenv("XDG_CACHE_HOME", value) + assert dl._cache_root() == str(tmp_path / "rel-xdg" / "agentdescent") + + +def test_an_absolute_cache_root_is_forwarded_untouched(store, tmp_path, monkeypatch): + root = tmp_path / "already" / "absolute" + monkeypatch.setenv("AGENTDESCENT_CACHE", str(root)) + monkeypatch.delenv("XDG_CACHE_HOME", raising=False) + + env, _ = _launched_child_env(tmp_path, monkeypatch) + + assert env["AGENTDESCENT_CACHE"] == str(root)