Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions agentdescent/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <new>/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)
Expand Down
5 changes: 5 additions & 0 deletions changelog.d/1124-text-apply-missing-parent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Fixed
A text-kind `apply <run> --to <fresh>/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).
57 changes: 57 additions & 0 deletions tests/test_cli_apply_text_missing_parent.py
Original file line number Diff line number Diff line change
@@ -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}: ")
Loading