From 7fc2db14439023875807d5a18e52e3c785564dd6 Mon Sep 17 00:00:00 2001 From: Danyang Chen Date: Mon, 23 Nov 2026 00:24:07 +0800 Subject: [PATCH] fix(cli): refuse a tree child blocked by a file before writing show --diff and apply --dry-run advertised a leaf as written when an intermediate path was already a regular file; the real apply then exited 2 with File exists. Check every produced path, not just dest, so the preview names the blocker and writes nothing. Closes #1221 --- agentdescent/evolution.py | 8 +- changelog.d/1221-blocked-child.md | 6 ++ docs/plugins.md | 4 +- tests/test_apply_blocked_child.py | 160 ++++++++++++++++++++++++++++++ 4 files changed, 176 insertions(+), 2 deletions(-) create mode 100644 changelog.d/1221-blocked-child.md create mode 100644 tests/test_apply_blocked_child.py diff --git a/agentdescent/evolution.py b/agentdescent/evolution.py index 65f3e625..3506f14a 100644 --- a/agentdescent/evolution.py +++ b/agentdescent/evolution.py @@ -1598,7 +1598,8 @@ def _refuse_blocked_parent(dest: str) -> None: 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 + ``--to blocker/rule.md``. Called from ``_check_apply_paths`` on ``dest`` + 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. """ @@ -1624,8 +1625,13 @@ def _check_apply_paths(dest: str, written: Sequence[str], backup: Optional[str], 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). """ _refuse_blocked_parent(dest) + for path in written: + _refuse_blocked_parent(path) if check_write is not None: check_write(dest, "to") for path in written: diff --git a/changelog.d/1221-blocked-child.md b/changelog.d/1221-blocked-child.md new file mode 100644 index 00000000..609631d9 --- /dev/null +++ b/changelog.d/1221-blocked-child.md @@ -0,0 +1,6 @@ +Fixed +`show --diff` and `apply --dry-run` no longer advertise a tree leaf whose +intermediate path is already a regular file; the preview and the real +`apply` now name the blocking file (`cannot write : '' is a +file, not a directory`) instead of listing the leaf as `written` and then +failing the real write with `File exists` (#1221). diff --git a/docs/plugins.md b/docs/plugins.md index 6984bb1a..47a3d567 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -283,7 +283,9 @@ all ~39 tools as the primary path. its `apply_plan` reports that refusal instead of a write `apply` would not 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, and a tree whose root is a symlink (#1204). + 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). * **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_child.py b/tests/test_apply_blocked_child.py new file mode 100644 index 00000000..50fa1553 --- /dev/null +++ b/tests/test_apply_blocked_child.py @@ -0,0 +1,160 @@ +"""A tree leaf blocked by a regular file is refused before anything is written (#1221). + +``#1208`` / ``#1215`` taught every apply surface to refuse a destination whose +nearest existing ancestor is a file. The hole one level down is a *child* of +an otherwise valid tree root: ``dest`` is a directory, ``dest/alpha`` is a +regular file, and the artifact writes ``alpha/SKILL.md``. ``_refuse_blocked_parent`` +inspected only ``dest``, so ``show --diff`` and ``apply --dry-run`` advertised +the leaf as ``written`` while the real ``apply`` exited 2 with +``cannot write : File exists``. + +The same ``_check_apply_paths`` funnel now walks every produced path, not just +the root, so the four surfaces agree on the blocking-file sentence and the +preview still writes nothing. ``apply --install-to`` shares that funnel and is +covered here, because no earlier test named the flag on a blocked destination. +""" + +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): + p = tmp_path / "blocker" + p.write_text("keep me\n", encoding="utf-8") + return p + + +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): + 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"] + + +def test_tree_child_blocked_by_a_file_is_named_on_all_four_surfaces(tmp_path, capsys): + dest = tmp_path / "lib" + dest.mkdir() + blocker = dest / "alpha" + blocker.write_text("keep me\n", encoding="utf-8") + store, rid = _tree_run(tmp_path, dest, state={"alpha/SKILL.md": "evolved\n"}) + child = dest / "alpha" / "SKILL.md" + expected = _blocked_message(child, 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 blocker.read_text(encoding="utf-8") == "keep me\n" + assert not child.exists() + assert sorted(p.name for p in dest.iterdir()) == listing + + +def test_a_sibling_regular_file_does_not_block_a_plain_leaf(tmp_path, capsys): + dest = tmp_path / "lib" + dest.mkdir() + sibling = dest / "sibling.md" + sibling.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.read_text(encoding="utf-8") == "leave me\n" + + +def test_apply_install_to_refuses_a_blocked_dest_and_writes_a_clean_one( + tmp_path, capsys): + blocker = _blocker(tmp_path) + blocked = blocker / "lib" + store, rid = _tree_run(tmp_path, blocked) + + err = _apply_error( + ["--store", store, "apply", rid, "--install-to", str(blocked), "--json"], + capsys) + assert err == _blocked_message(blocked, blocker) + assert blocker.read_text(encoding="utf-8") == "keep me\n" + assert not blocked.exists() + + clean = tmp_path / "skills" + 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" / "SKILL.md").read_text(encoding="utf-8") == "evolved\n" + assert (clean / "beta" / "SKILL.md").read_text(encoding="utf-8") == "evolved\n"