diff --git a/agentdescent/evolution.py b/agentdescent/evolution.py index 3506f14a..a3adf095 100644 --- a/agentdescent/evolution.py +++ b/agentdescent/evolution.py @@ -1602,6 +1602,8 @@ def _refuse_blocked_parent(dest: str) -> None: and every path in ``written`` (tree children included, #1221) so every surface that plans (``show`` text, ``apply`` text, ``--from``, ``--install-to`` and the tree writer) refuses before anything is created. + The inverse -- a directory already sitting at a written leaf -- is + ``_refuse_blocked_leaf`` (#1223). """ parent = os.path.dirname(dest.rstrip(os.sep)) or os.curdir cur = parent @@ -1618,20 +1620,47 @@ def _refuse_blocked_parent(dest: str) -> None: f"cannot write {dest}: {cur!r} is a file, not a directory") +def _refuse_blocked_leaf(path: str) -> None: + """Refuse a write whose leaf already exists as a directory (#1223). + + ``_refuse_blocked_parent`` catches a regular file sitting on an intermediate + path. The inverse is a directory sitting where the artifact wants a file: + ``dest`` is valid, ``dest/alpha`` is a directory, and the tree writes + ``alpha``. ``show --diff`` and ``apply --dry-run`` then advertised the leaf + as ``written`` while the real ``apply`` died at ``open`` with + ``cannot write : Is a directory`` -- the tree root, not the blocking + leaf. ``apply --from`` was worse: ``shutil.copy2`` nested the bytes at + ``dest/alpha/alpha`` and reported success. + + Called from ``_check_apply_paths`` on every path in ``written`` (not on + ``dest`` itself: a tree root *is* a directory). A symlink at the leaf is + left to ``_refuse_dest_link`` / ``dest_problem``; this helper names only a + real directory. + """ + if os.path.lexists(path) and not os.path.islink(path) and os.path.isdir(path): + from . import runstore # runstore imports this module at import time + + raise runstore.RunStoreError( + f"cannot write {path}: {path!r} is a directory, not a file") + + 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. - 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. - It covers ``dest`` and every path in ``written`` (tree children included): - a regular file sitting at an intermediate path blocks a leaf the same way - a file sitting at the destination's parent does (#1221, #1208). + The blocked-parent and blocked-leaf refusals run before the operator + allowlist guard: they are a property of the filesystem, not of who is + writing, so ``show`` and ``--dry-run`` see them even when + ``AGENTDESCENT_SURFACE_WRITE_DIRS`` is unset. The helper covers ``dest`` + and every path in ``written`` (tree children included): a regular file + sitting at an intermediate path blocks a leaf the same way a file sitting + at the destination's parent does (#1221, #1208), and a directory already + sitting at a written leaf is refused the other way around (#1223). """ _refuse_blocked_parent(dest) for path in written: _refuse_blocked_parent(path) + _refuse_blocked_leaf(path) if check_write is not None: check_write(dest, "to") for path in written: diff --git a/changelog.d/1223-blocked-leaf.md b/changelog.d/1223-blocked-leaf.md new file mode 100644 index 00000000..2d1b84fe --- /dev/null +++ b/changelog.d/1223-blocked-leaf.md @@ -0,0 +1,7 @@ +Fixed +`show --diff` and `apply --dry-run` no longer advertise a tree leaf that is +already a directory, and `apply --from` no longer writes inside that +directory; the preview and the real `apply` now name the blocking path +(`cannot write : '' is a directory, not a file`) instead of +listing the leaf as `written` and then failing the real write with +`Is a directory`, or nesting a file at `/` (#1223). diff --git a/docs/plugins.md b/docs/plugins.md index 47a3d567..4ea0d5e7 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -284,8 +284,9 @@ all ~39 tools as the primary path. perform (#1188). The same preview also reports the destination refusals `apply` raises before that check: a text artifact whose target is a directory or a symlink, a tree whose root is a symlink (#1204), a - destination whose nearest existing ancestor is a file (#1208), and a - tree child blocked by a regular file on the way to a leaf (#1221). + destination whose nearest existing ancestor is a file (#1208), a + tree child blocked by a regular file on the way to a leaf (#1221), + and a tree leaf that is already a directory (#1223). * **Questions about an archive are read-only tools.** `safety` (safe to keep seeding from?), `portfolio` (layer ranking and a suggested budget split) and `audit_trace` (one adoption's evidence rings) return the same JSON as the diff --git a/tests/test_apply_blocked_leaf.py b/tests/test_apply_blocked_leaf.py new file mode 100644 index 00000000..6488bf1b --- /dev/null +++ b/tests/test_apply_blocked_leaf.py @@ -0,0 +1,229 @@ +"""A tree leaf that is already a directory is refused before anything is written (#1223). + +``#1221`` / ``#1222`` taught every apply surface to refuse a regular file sitting +on the way to a leaf. The inverse hole is a *directory* sitting where the +artifact wants a file: ``dest`` is a directory, ``dest/alpha`` is a directory +(with ``keep.txt`` inside), and the artifact writes ``alpha``. ``show --diff`` +and ``apply --dry-run`` advertised the leaf as ``written`` while the real +``apply`` exited 2 with ``cannot write : Is a directory`` -- the tree +root, not the blocking leaf. + +``apply --from`` was worse: ``shutil.copy2`` dropped the bytes at +``dest/alpha/alpha`` and reported success. + +The same ``_check_apply_paths`` funnel now refuses a produced path that exists +as a real directory, so the four surfaces agree on the blocking-directory +sentence and the preview still writes nothing. ``apply --from`` and +``apply --install-to`` share that funnel. +""" + +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 _tree_run(tmp_path, dest, state=None): + store = str(tmp_path / "runs-tree") + if state is None: + state = {"alpha": "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(leaf): + return f"cannot write {leaf}: {str(leaf)!r} is a directory, not a file" + + +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"] + + +def test_tree_leaf_blocked_by_a_directory_is_named_on_all_four_surfaces( + tmp_path, capsys): + dest = tmp_path / "lib" + dest.mkdir() + blocker = dest / "alpha" + blocker.mkdir() + keep = blocker / "keep.txt" + keep.write_text("stay\n", encoding="utf-8") + store, rid = _tree_run(tmp_path, dest, state={"alpha": "evolved\n"}) + expected = _blocked_message(blocker) + listing = sorted(p.name for p in dest.iterdir()) + + 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 == expected + assert "written" not in plan + assert "would_write" not in plan + + code, out = _run_cli(["--store", store, "show", rid, "--diff"], capsys) + assert code == 0, out.err + assert show_error in out.out + + 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") + + assert keep.read_text(encoding="utf-8") == "stay\n" + assert blocker.is_dir() + assert not (blocker / "alpha").exists() + assert sorted(p.name for p in dest.iterdir()) == listing + + +def test_a_nested_leaf_directory_is_named_the_same_way(tmp_path, capsys): + dest = tmp_path / "lib" + dest.mkdir() + parent = dest / "beta" + parent.mkdir() + blocker = parent / "rule.md" + blocker.mkdir() + (blocker / "keep.txt").write_text("stay\n", encoding="utf-8") + store, rid = _tree_run(tmp_path, dest, state={"beta/rule.md": "evolved\n"}) + expected = _blocked_message(blocker) + + plan = _show_plan(store, rid, capsys) + assert plan.get("error") == expected + assert "written" not in plan + + err = _apply_error( + ["--store", store, "apply", rid, "--to", str(dest), "--json"], capsys) + assert err == expected + assert (blocker / "keep.txt").read_text(encoding="utf-8") == "stay\n" + assert blocker.is_dir() + + +def test_a_sibling_directory_does_not_block_a_plain_leaf(tmp_path, capsys): + dest = tmp_path / "lib" + dest.mkdir() + sibling = dest / "other" + sibling.mkdir() + (sibling / "keep.txt").write_text("leave me\n", encoding="utf-8") + store, rid = _tree_run(tmp_path, dest, state={"plain/written.md": "evolved\n"}) + + plan = _show_plan(store, rid, capsys) + assert "error" not in plan, json.dumps(plan) + assert plan["written"] == ["plain/written.md"] + assert plan["noop"] is False + + 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"] == ["plain/written.md"] + assert (dest / "plain" / "written.md").read_text(encoding="utf-8") == "evolved\n" + assert (sibling / "keep.txt").read_text(encoding="utf-8") == "leave me\n" + + +def test_apply_from_refuses_a_directory_at_the_leaf_and_writes_a_clean_one( + tmp_path, capsys): + src = tmp_path / "src" + src.mkdir() + (src / "alpha").write_text("evolved\n", encoding="utf-8") + + dest = tmp_path / "lib" + dest.mkdir() + blocker = dest / "alpha" + blocker.mkdir() + keep = blocker / "keep.txt" + keep.write_text("stay\n", encoding="utf-8") + expected = _blocked_message(blocker) + + dry_error = _apply_error( + ["apply", "--from", str(src), "--to", str(dest), "--dry-run", "--json"], + capsys) + assert dry_error == expected + assert keep.read_text(encoding="utf-8") == "stay\n" + assert not (blocker / "alpha").exists() + + real_error = _apply_error( + ["apply", "--from", str(src), "--to", str(dest), "--json"], capsys) + assert real_error == dry_error + assert keep.read_text(encoding="utf-8") == "stay\n" + assert blocker.is_dir() + assert not (blocker / "alpha").exists() + # No backup of a write that was refused. + home = tmp_path / "ad-home" + assert not (home / "backups").exists() or not any(home.joinpath("backups").rglob("*")) + + clean = tmp_path / "clean" + clean.mkdir() + code, out = _run_cli( + ["apply", "--from", str(src), "--to", str(clean), "--json"], capsys) + assert code == 0, out.err or out.out + assert json.loads(out.out)["written"] == ["alpha"] + assert (clean / "alpha").read_text(encoding="utf-8") == "evolved\n" + assert (clean / "alpha").is_file() + + +def test_apply_install_to_refuses_a_directory_at_the_leaf(tmp_path, capsys): + dest = tmp_path / "skills" + dest.mkdir() + blocker = dest / "alpha" + blocker.mkdir() + keep = blocker / "keep.txt" + keep.write_text("stay\n", encoding="utf-8") + store, rid = _tree_run(tmp_path, dest, state={"alpha": "evolved\n"}) + expected = _blocked_message(blocker) + + err = _apply_error( + ["--store", store, "apply", rid, "--install-to", str(dest), "--json"], + capsys) + assert err == expected + assert keep.read_text(encoding="utf-8") == "stay\n" + assert blocker.is_dir() + assert not (blocker / "alpha").exists() + + clean = tmp_path / "skills-clean" + code, out = _run_cli( + ["--store", store, "apply", rid, "--install-to", str(clean), "--json"], + capsys) + assert code == 0, out.err or out.out + assert (clean / "alpha").read_text(encoding="utf-8") == "evolved\n" + assert (clean / "alpha").is_file()