From 62e8dd00b33804b79ca0f3aa2e7d06194bfe48f3 Mon Sep 17 00:00:00 2001 From: HugoXK Date: Wed, 25 Nov 2026 04:42:51 +0000 Subject: [PATCH 1/3] fix(dataloader): read a relative cache root back absolute AGENTDESCENT_CACHE and XDG_CACHE_HOME were expanded but not made absolute, so cache_path handed back (and created) a bare relative directory: the same half-expansion #402 fixed for the run store, left in the dataset cache after #1261. hf_rows_modelscope's docstring still promised one fixed ~/.cache/agentdescent/modelscope/ path, so documented and written roots disagreed once the knob moved. Refs #1280 --- agentdescent/dataloader.py | 22 ++++--- changelog.d/1280-cache-root-abs.md | 7 +++ tests/test_dataloader_cache_root_abs.py | 82 +++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 7 deletions(-) create mode 100644 changelog.d/1280-cache-root-abs.md create mode 100644 tests/test_dataloader_cache_root_abs.py 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/changelog.d/1280-cache-root-abs.md b/changelog.d/1280-cache-root-abs.md new file mode 100644 index 00000000..52158935 --- /dev/null +++ b/changelog.d/1280-cache-root-abs.md @@ -0,0 +1,7 @@ +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, so the dataset cache root is one spelled +path however a later caller was launched. `hf_rows_modelscope`'s docstring +names both knobs instead of one fixed `~/.cache/agentdescent/modelscope/` +(#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..4cd27de5 --- /dev/null +++ b/tests/test_dataloader_cache_root_abs.py @@ -0,0 +1,82 @@ +"""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): + """Whichever process resolves it -- the CLI or the detached child in the run dir. + + The value is still resolved against the cwd of the process that reads it + (the same contract ``#403`` gave the run store), so the path is absolute in + both, and ``cache_path`` never hands back a bare relative path. + """ + 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 From 438aee62424b4b8296a5acb8a9adf278e701479e Mon Sep 17 00:00:00 2001 From: HugoXK Date: Wed, 25 Nov 2026 05:03:15 +0000 Subject: [PATCH 2/3] fix(runstore): hand the detached child an absolute dataset cache root `launch` scrubs the environment but keeps `AGENTDESCENT_*` and starts the child at `cwd=rd.path`, so a relative `AGENTDESCENT_CACHE` or `XDG_CACHE_HOME` named one tree in the process that launched the run and another in the run itself: the cache moved with every run directory, which is the bug #1280 is about. Only the launcher still knows the cwd the value was typed in, so both knobs are spelled absolute there when they are relative -- after the caller's `env` is merged, before the Ticket marker handling. An absolute value is forwarded untouched and `PYTHONPATH` is not involved. #402/#403 normalised the store the same way. Verified with Product's `probe_child_cache_root.py`, which asks a real child for `_cache_root()`: `one_tree: false` for both knobs at 62e8dd00, `one_tree: true` at this tip with an absolute `value_in_child_env`. --- agentdescent/runstore.py | 12 ++++ changelog.d/1280-cache-root-abs.md | 6 +- tests/test_dataloader_cache_root_abs.py | 13 ++-- tests/test_runstore_cache_env.py | 90 +++++++++++++++++++++++++ 4 files changed, 114 insertions(+), 7 deletions(-) create mode 100644 tests/test_runstore_cache_env.py 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 index 52158935..093b468c 100644 --- a/changelog.d/1280-cache-root-abs.md +++ b/changelog.d/1280-cache-root-abs.md @@ -3,5 +3,7 @@ Fixed the same half `#403` gave the run store: `cache_path()` no longer returns (or creates) a bare relative directory, so the dataset cache root is one spelled path however a later caller was launched. `hf_rows_modelscope`'s docstring -names both knobs instead of one fixed `~/.cache/agentdescent/modelscope/` -(#1280). +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 index 4cd27de5..d38b2132 100644 --- a/tests/test_dataloader_cache_root_abs.py +++ b/tests/test_dataloader_cache_root_abs.py @@ -43,11 +43,14 @@ def test_relative_xdg_cache_home_resolves_absolute(tmp_path, monkeypatch): def test_relative_root_is_absolute_whatever_the_cwd(tmp_path, monkeypatch): - """Whichever process resolves it -- the CLI or the detached child in the run dir. - - The value is still resolved against the cwd of the process that reads it - (the same contract ``#403`` gave the run store), so the path is absolute in - both, and ``cache_path`` never hands back a bare relative path. + """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() 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) From 7fbcc3dc298bfb0e671f22fc74257585df00c89a Mon Sep 17 00:00:00 2001 From: HugoXK Date: Wed, 25 Nov 2026 05:55:05 +0000 Subject: [PATCH 3/3] docs(changelog): stop promising one spelled cache root for every caller The fragment said the dataset cache root is one spelled path "however a later caller was launched", which is false outside `launch`: a caller that builds its own environment by hand and reads a relative value from another cwd still spells a different path to the same tree -- the `#403` contract the PR body states narrowly. Release notes are read as promises, so the fragment now stops at "a bare relative directory"; its last sentence already scopes the launch claim. Docs-only: one changelog fragment, no code or test change. Refs #1280. --- changelog.d/1280-cache-root-abs.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/changelog.d/1280-cache-root-abs.md b/changelog.d/1280-cache-root-abs.md index 093b468c..d487ffa5 100644 --- a/changelog.d/1280-cache-root-abs.md +++ b/changelog.d/1280-cache-root-abs.md @@ -1,9 +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, so the dataset cache root is one spelled -path however a later caller was launched. `hf_rows_modelscope`'s docstring -names both knobs instead of one fixed `~/.cache/agentdescent/modelscope/`, and +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).