diff --git a/agentdescent/evolution.py b/agentdescent/evolution.py index e1c4a86c..443530f2 100644 --- a/agentdescent/evolution.py +++ b/agentdescent/evolution.py @@ -1579,9 +1579,46 @@ def _file_tree_backup_path(dest: str) -> str: return os.path.join(base, f"bak-{n}") +def _refuse_blocked_parent(dest: str) -> None: + """Refuse a destination whose nearest existing ancestor is not a directory (#1208). + + ``apply`` creates a missing parent only after the plan is returned: the text + branch and ``--from`` call ``os.makedirs`` just before ``open``, and a tree + goes through ``filetree.materialize``. ``show --diff`` and ``apply + --dry-run`` therefore never reached the step that fails, and the real write + reported ``cannot write : File exists`` -- the parent's errno printed + against a destination that never existed. Name the blocker instead. + + Walking up to the nearest *existing* ancestor, not just the immediate + parent, is what makes ``--to blocker/a/b/rule.md`` fail like + ``--to blocker/rule.md``. Called from ``_check_apply_paths`` so every + surface that plans (``show`` text, ``apply`` text, ``--from``, + ``--install-to`` and the tree writer) refuses before anything is created. + """ + parent = os.path.dirname(dest.rstrip(os.sep)) or os.curdir + cur = parent + while not os.path.lexists(cur): + nxt = os.path.dirname(cur) + if nxt == cur: + cur = os.curdir + break + cur = nxt + if os.path.lexists(cur) and not os.path.isdir(cur): + from . import runstore # runstore imports this module at import time + + raise runstore.RunStoreError( + f"cannot write {dest}: {cur!r} is a file, not a directory") + + def _check_apply_paths(dest: str, written: Sequence[str], backup: Optional[str], check_write: Optional[Callable[[str, str], Any]]) -> None: - """Check the selected write set before any artifact or backup is changed.""" + """Check the selected write set before any artifact or backup is changed. + + The blocked-parent refusal runs before the operator allowlist guard: it is a + property of the filesystem, not of who is writing, so ``show`` and + ``--dry-run`` see it even when ``AGENTDESCENT_SURFACE_WRITE_DIRS`` is unset. + """ + _refuse_blocked_parent(dest) if check_write is not None: check_write(dest, "to") for path in written: diff --git a/changelog.d/1208-blocked-parent.md b/changelog.d/1208-blocked-parent.md new file mode 100644 index 00000000..42504894 --- /dev/null +++ b/changelog.d/1208-blocked-parent.md @@ -0,0 +1,6 @@ +Fixed +`agentdescent show --diff` and `apply --dry-run` no longer advertise a write +whose nearest existing parent is a regular file; the preview and the real +`apply` now name the blocking file (`cannot write : '' is a +file, not a directory`) instead of `File exists` on a destination that never +existed (#1208). diff --git a/tests/test_apply_blocked_parent.py b/tests/test_apply_blocked_parent.py new file mode 100644 index 00000000..b6020331 --- /dev/null +++ b/tests/test_apply_blocked_parent.py @@ -0,0 +1,203 @@ +"""A destination under a regular file is refused before anything is planned (#1208). + +``apply`` materialises a missing parent only after the planning path returns: +the text branch creates it just before ``open`` (``cli.py``), ``--from`` does +the same, and a tree goes through ``filetree.materialize``. ``show --diff`` and +``apply --dry-run`` therefore never reached the failing step and advertised a +write that the real ``apply`` exited 2 on -- with ``cannot write : File +exists``, an errno belonging to the **parent**, printed against a destination +that never existed. + +``_check_apply_paths`` is the one funnel every surface already uses (``show`` +text, ``apply`` text, ``--from``, ``--install-to`` and the tree writer), so the +refusal lives there and the three surfaces agree by construction. The message +names the blocking ancestor instead of copying the pre-existing text; that is +the intended change for this case, not a ``#1204`` regression (whose "message +bytes unchanged" rule is about the destination refusals). +""" + +from __future__ import annotations + +import json + +import pytest + +from agentdescent import runstore +from agentdescent.cli import main +from agentdescent.evolution import EvolutionResult +from agentdescent.filetree import canonical + + +@pytest.fixture(autouse=True) +def isolated_home(tmp_path, monkeypatch): + monkeypatch.setenv("AGENTDESCENT_HOME", str(tmp_path / "ad-home")) + monkeypatch.delenv("AGENTDESCENT_SURFACE_WRITE_DIRS", raising=False) + + +def _run_cli(argv, capsys): + code = main(argv) + return code, capsys.readouterr() + + +def _blocker(tmp_path): + """A regular file where a directory has to be; unwritable whatever the uid.""" + p = tmp_path / "blocker" + p.write_text("keep me\n", encoding="utf-8") + return p + + +def _text_run(tmp_path, dest, rendered="hello\n"): + store = str(tmp_path / "runs-text") + rd = runstore.create({ + "kind": "text", "target": str(dest), + "data": {"inline": [{"prompt": "p", "gold": "g"}], + "prompt": "prompt", "gold": "gold"}, + "score": "exact", "agent": {"ref": "echo"}, "reflect": {"ref": "echo"}, + }, store=store) + EvolutionResult(state={}, rendered=rendered, final_reward=1.0, + history=[], ledger_log=[]).save(rd.result_path) + rd.update_status(state="done") + return store, str(rd.run_id) + + +def _tree_run(tmp_path, dest, state=None): + store = str(tmp_path / "runs-tree") + if state is None: + state = {"alpha/SKILL.md": "evolved\n", "beta/SKILL.md": "evolved\n"} + rd = runstore.create({ + "kind": "skill_library", + "target": str(dest), + "data": {"inline": [{"prompt": "p", "gold": "g"}], + "prompt": "prompt", "gold": "gold"}, + "score": "exact", + "agent": {"ref": "claude_code"}, + "reflect": {"ref": "echo"}, + }, store=store) + EvolutionResult(state=state, rendered=canonical(state), final_reward=1.0, + history=[], ledger_log=[]).save(rd.result_path) + rd.update_status(state="done") + return store, str(rd.run_id) + + +def _blocked_message(dest, blocker): + """The one message all three surfaces must carry, derived from the paths.""" + return f"cannot write {dest}: {str(blocker)!r} is a file, not a directory" + + +def _show_plan(store, rid, capsys): + code, out = _run_cli( + ["--store", store, "show", rid, "--diff", "--json"], capsys) + assert code == 0, out.err or out.out + return json.loads(out.out)["apply_plan"] + + +def _apply_error(argv, capsys): + code, out = _run_cli(argv, capsys) + assert code == 2, out.out or out.err + doc = json.loads(out.out) + assert doc["ok"] is False + return doc["error"] + + +@pytest.mark.parametrize("rel", ["rule.md", "a/b/rule.md"], + ids=["direct-parent", "deep-parent"]) +def test_text_show_dry_run_and_apply_name_the_blocking_parent(tmp_path, capsys, + rel): + blocker = _blocker(tmp_path) + dest = blocker / rel + store, rid = _text_run(tmp_path, dest) + assert not dest.exists() + + plan = _show_plan(store, rid, capsys) + show_error = plan.get("error") + assert show_error, ( + "show advertised a write the real apply refuses: " + json.dumps(plan)) + assert show_error == _blocked_message(dest, blocker) + + dry_error = _apply_error( + ["--store", store, "apply", rid, "--to", str(dest), "--dry-run", "--json"], + capsys) + assert dry_error == show_error + + real_error = _apply_error( + ["--store", store, "apply", rid, "--to", str(dest), "--json"], capsys) + assert real_error == show_error, ( + "the preview copied a message apply does not produce") + + # The human `show --diff` prints the same plan, so the line is not JSON-only. + code, out = _run_cli(["--store", store, "show", rid, "--diff"], capsys) + assert code == 0, out.err + assert show_error in out.out + + # Read-only preview, untouched blocker, nothing created in between. + assert blocker.read_text(encoding="utf-8") == "keep me\n" + assert not dest.exists() + + +def test_tree_show_dry_run_and_apply_name_the_blocking_parent(tmp_path, capsys): + blocker = _blocker(tmp_path) + dest = blocker / "lib" + store, rid = _tree_run(tmp_path, dest) + + plan = _show_plan(store, rid, capsys) + show_error = plan.get("error") + assert show_error, ( + "show advertised a tree the real apply refuses: " + json.dumps(plan)) + assert show_error == _blocked_message(dest, blocker) + + dry_error = _apply_error( + ["--store", store, "apply", rid, "--to", str(dest), "--dry-run", "--json"], + capsys) + assert dry_error == show_error + + real_error = _apply_error( + ["--store", store, "apply", rid, "--to", str(dest), "--json"], capsys) + assert real_error == show_error + + assert blocker.read_text(encoding="utf-8") == "keep me\n" + assert not dest.exists() + + +def test_apply_from_names_the_blocking_parent(tmp_path, capsys): + blocker = _blocker(tmp_path) + dest = blocker / "rule.md" + source = tmp_path / "incoming.md" + source.write_text("from file\n", encoding="utf-8") + + dry_error = _apply_error( + ["apply", "--from", str(source), "--to", str(dest), "--dry-run", "--json"], + capsys) + assert dry_error == _blocked_message(dest, blocker) + + real_error = _apply_error( + ["apply", "--from", str(source), "--to", str(dest), "--json"], capsys) + assert real_error == dry_error + + assert blocker.read_text(encoding="utf-8") == "keep me\n" + assert source.read_text(encoding="utf-8") == "from file\n" + assert not dest.exists() + + +def test_a_creatable_parent_still_writes(tmp_path, capsys): + """The #1125 control: a missing parent with no blocker is not a refusal.""" + dest = tmp_path / "newdir" / "rule.md" + store, rid = _text_run(tmp_path, dest) + + plan = _show_plan(store, rid, capsys) + assert "error" not in plan, json.dumps(plan) + assert plan["written"] == [str(dest)] + assert plan["noop"] is False + + code, out = _run_cli( + ["--store", store, "apply", rid, "--to", str(dest), "--dry-run", "--json"], + capsys) + assert code == 0, out.err or out.out + assert json.loads(out.out)["written"] == [str(dest)] + assert not dest.parent.exists(), "a dry run writes nothing, parents included" + + code, out = _run_cli( + ["--store", store, "apply", rid, "--to", str(dest), "--json"], capsys) + assert code == 0, out.err or out.out + assert json.loads(out.out)["written"] == [str(dest)] + assert dest.read_text(encoding="utf-8") == "hello\n" + assert dest.parent.is_dir()