From 9e0c669b73e995fc39105cd0028dfa67dc8493a8 Mon Sep 17 00:00:00 2001 From: HugoXK Date: Mon, 16 Nov 2026 09:24:40 +0000 Subject: [PATCH] fix(cli): text `apply --to` creates a missing parent (#1124) `_text_plan` only asks whether `dest` already holds the evolved text, so a missing parent was invisible to the dry run: it printed `written` with exit 0 and the real run died at `open` with `error: cannot write ...: No such file or directory`, writing nothing. The tree branch materialises its parents and so does the `--from` branch; the run-store text branch now does the same before `open`, and an `OSError` from `makedirs` still takes the existing `_cannot_write` single-line path. The reverse row pins the #990 contract (exit 2, one `error: cannot write ...` line); `tests/test_cli_write_error.py` is unchanged. Not in scope: the tree branch, `--from`, `_text_plan`'s noop rules. --- agentdescent/cli.py | 11 ++++ changelog.d/1124-text-apply-missing-parent.md | 5 ++ tests/test_cli_apply_text_missing_parent.py | 57 +++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 changelog.d/1124-text-apply-missing-parent.md create mode 100644 tests/test_cli_apply_text_missing_parent.py diff --git a/agentdescent/cli.py b/agentdescent/cli.py index c4db5c7b..05749646 100644 --- a/agentdescent/cli.py +++ b/agentdescent/cli.py @@ -3418,6 +3418,17 @@ def apply_payload(run_id: str, *, to: Optional[str] = None, store: Optional[str] except OSError as e: raise _cannot_write(slot, e) from None saved = [slot] + # `_text_plan` only asks whether `dest` already holds the text, so a + # missing parent is invisible to the dry run and the real write died at + # `open` (#1124). The tree branch materialises its parents, and so does + # the `--from` branch below; a fresh `--to /rule.md` must not be the + # one shape that refuses. + parent = os.path.dirname(dest) + try: + if parent: + os.makedirs(parent, exist_ok=True) + except OSError as e: + raise _cannot_write(dest, e) from None try: with open(dest, "w", encoding="utf-8", newline="") as fh: fh.write(result.rendered) diff --git a/changelog.d/1124-text-apply-missing-parent.md b/changelog.d/1124-text-apply-missing-parent.md new file mode 100644 index 00000000..fe293d88 --- /dev/null +++ b/changelog.d/1124-text-apply-missing-parent.md @@ -0,0 +1,5 @@ +Fixed +A text-kind `apply --to /rule.md` now creates the missing parent +directory before writing, so the dry run it had already reported green is true: +the real run exits 0 and the file lands at that path. A genuinely unwritable +`--to` still exits 2 with the single `error: cannot write …` line (#1124). diff --git a/tests/test_cli_apply_text_missing_parent.py b/tests/test_cli_apply_text_missing_parent.py new file mode 100644 index 00000000..3501ee53 --- /dev/null +++ b/tests/test_cli_apply_text_missing_parent.py @@ -0,0 +1,57 @@ +"""A text ``apply --to`` into a missing parent writes, like the tree branch (#1124). + +The dry run asked ``_text_plan`` only whether ``dest`` already held the text, so a +missing parent was invisible to it: the plan said ``written`` and the real run +died at ``open`` with ``error: cannot write ...: No such file or directory``, +writing nothing. ``apply --from`` already creates the parent before ``open`` +(``cli.py``, the ``--from`` branch); this is the run-store branch catching up, +not a second rule. + +The reverse row is a pin, not a red: a genuinely unwritable ``--to`` must keep +the one-line refusal from `#990` -- see ``tests/test_cli_write_error.py``, whose +rows are left exactly as they are. +""" + +from __future__ import annotations + +import json + +from agentdescent import cli + +from tests.test_cli_write_error import _text_run + + +def test_a_text_apply_creates_a_missing_parent(tmp_path, capsys): + """Dry run true, real run 0: the file lands and its parent is made.""" + store, rid = _text_run(tmp_path) + dest = tmp_path / "newdir" / "rule.md" + assert not dest.parent.exists() + + assert cli.main(["--store", store, "apply", rid, "--to", str(dest), + "--dry-run"]) == 0 + plan = json.loads(capsys.readouterr().out) + assert plan["written"] == [str(dest)] + assert plan["noop"] is False + assert not dest.parent.exists(), "a dry run writes nothing, parents included" + + assert cli.main(["--store", store, "apply", rid, "--to", str(dest)]) == 0 + assert dest.read_text(encoding="utf-8") == "evolved rule\n" + assert dest.parent.is_dir() + + +def test_a_text_apply_into_an_unwritable_parent_is_still_one_line(tmp_path, + capsys): + """Pin #990 for the text branch: exit 2, one line, no traceback.""" + store, rid = _text_run(tmp_path) + blocked = tmp_path / "blocked" + blocked.write_text("not a directory\n", encoding="utf-8") + dest = blocked / "rule.md" + + assert cli.main(["--store", store, "apply", rid, "--to", str(dest)]) == 2 + + cap = capsys.readouterr() + assert "Traceback" not in cap.err and "Traceback" not in cap.out + assert cap.out == "" + lines = [ln for ln in cap.err.strip().splitlines() if ln] + assert len(lines) == 1, cap.err + assert lines[0].startswith(f"error: cannot write {dest}: ")