Skip to content
Open
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
32 changes: 22 additions & 10 deletions agentdescent/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3091,20 +3091,24 @@ def _partial_show(rd: "runstore.RunDir", st: Any) -> Dict[str, Any]:


def _refuse_plan_outside_write_dirs(payload: Dict[str, Any],
target: Optional[str]) -> None:
target: Optional[str],
written: Sequence[str] = (),
backup: Optional[str] = None) -> None:
"""Say so when the boundary would refuse the write ``apply_plan`` describes.

``show`` writes nothing, so it still renders the diff; what it must not do is
print a plan naming a file ``apply`` would then refuse (#1082).
print a plan naming a file ``apply`` would then refuse (#1082, #1188). The
check set is the one ``apply`` uses for a single-file artifact: the
destination, every path in ``written``, and the ``.bak-N`` slot when one
would be taken. A tree artifact gets the same set from ``_write_to`` called
with ``check_write``, which is the call ``apply`` makes for a tree.
"""
plan = payload.get("apply_plan")
if not target or not isinstance(plan, dict) or "error" in plan:
return
guard = _operator_write_guard()
if guard is None:
return
try:
guard(os.path.expanduser(target), "to")
_check_apply_paths(os.path.expanduser(target), list(written), backup,
_operator_write_guard())
except runstore.RunStoreError as e:
payload["apply_plan"] = {"error": str(e)}

Expand Down Expand Up @@ -3181,8 +3185,12 @@ def show_payload(run_id: str, *, store: Optional[str] = None, diff: bool = True,
return payload
if spec.kind == "text":
payload["rendered"] = result.rendered[:max_chars]
payload["apply_plan"] = _text_plan(os.path.expanduser(spec.target), result.rendered)
_refuse_plan_outside_write_dirs(payload, spec.target)
dest = os.path.expanduser(spec.target)
plan = _text_plan(dest, result.rendered)
payload["apply_plan"] = plan
_refuse_plan_outside_write_dirs(
payload, dest, plan["written"],
_backup_slot(dest) if not plan["noop"] and os.path.exists(dest) else None)
return payload
payload["files"] = sorted(result.state)
if diff:
Expand All @@ -3192,11 +3200,15 @@ def show_payload(run_id: str, *, store: Optional[str] = None, diff: bool = True,
except Exception as e: # noqa: BLE001 - the original may have moved
before, payload["diff_note"] = {}, f"could not read original {spec.target}: {e}"
payload["diff"] = _tree_diff(before, result.state)[:max_chars]
# The call `apply` makes for a tree, with only `dry_run` differing:
# `_write_to` checks the destination, every written leaf and the
# directory backup slot before it returns the plan (#1188).
try:
payload["apply_plan"] = result.write_to(os.path.expanduser(spec.target), dry_run=True)
payload["apply_plan"] = result._write_to(
os.path.expanduser(spec.target), dry_run=True,
check_write=_operator_write_guard())
except Exception as e: # noqa: BLE001
payload["apply_plan"] = {"error": str(e)}
_refuse_plan_outside_write_dirs(payload, spec.target)
return payload


Expand Down
6 changes: 6 additions & 0 deletions changelog.d/1188-show-preview-write-dirs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Fixed
`agentdescent show --diff` previews the same write `apply` performs: when the
directory backup a plan would take sits outside
`AGENTDESCENT_SURFACE_WRITE_DIRS`, its `apply_plan` reports that refusal --
naming the path and the allowed roots -- instead of listing the same files as
`written` (#1188).
6 changes: 5 additions & 1 deletion docs/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,11 @@ all ~39 tools as the primary path.
(`AGENTDESCENT_NESTED=1`) the allowlist defaults to the run's cwd plus
`$AGENTDESCENT_HOME` even when it is unset; outside a worker it is only
enforced when the variable is set. A refused write exits 2 before anything,
including the `.bak-N` copy, touches disk.
including the `.bak-N` copy, touches disk. `show --diff` checks the same set
there -- the destination, every file its plan names, and the backup slot
(a directory artifact backs up under `$AGENTDESCENT_HOME/backups/...`) -- so
its `apply_plan` reports that refusal instead of a write `apply` would not
perform (#1188).
* **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
Expand Down
102 changes: 102 additions & 0 deletions tests/test_write_dirs_apply_show.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
"""``show --diff`` must not advertise the write ``apply`` refuses (#1188).

``apply`` checks the destination, every written leaf and the backup slot before
anything touches disk. A directory artifact backs up under
``$AGENTDESCENT_HOME/backups/...`` -- not beside the target -- so a destination
inside ``AGENTDESCENT_SURFACE_WRITE_DIRS`` whose home is outside it is a write
``apply`` refuses. ``show`` used to check only the destination and report that
same plan as ``written``: a preview for a call that exits 2.
"""

import json

import pytest

from agentdescent import runstore
from agentdescent.cli import NESTED_ENV, main
from agentdescent.evolution import EvolutionResult
from agentdescent.filetree import canonical
from tests.test_skill_library_apply_install import _make_library_run


@pytest.fixture(autouse=True)
def isolated_home(tmp_path, monkeypatch):
# Directory backups land here; each test decides whether that is inside or
# outside the allowlist it sets.
monkeypatch.setenv("AGENTDESCENT_HOME", str(tmp_path / "ad-home"))


def _run_cli(argv, capsys):
code = main(argv)
return code, capsys.readouterr()


def test_show_refuses_a_plan_whose_directory_backup_is_outside_the_allowlist(
tmp_path, capsys, monkeypatch):
store, rid = _make_library_run(tmp_path)
dest = tmp_path / "seed-lib" # the run's target: inside the allowlist
home = tmp_path / "ad-home" # ... but its directory backup is not
monkeypatch.setenv("AGENTDESCENT_SURFACE_WRITE_DIRS", str(dest))
before = (dest / "alpha" / "SKILL.md").read_text(encoding="utf-8")

code, out = _run_cli(["--store", store, "show", rid, "--diff", "--json"], capsys)
assert code == 0
plan = json.loads(out.out)["apply_plan"]
assert "write allowlist" in plan.get("error", ""), (
"show advertised a write it cannot apply: " + json.dumps(plan))
assert str(home) in plan["error"] # the refused backup path is named ...
assert str(dest) in plan["error"] # ... beside the allowed root

code, out = _run_cli(["--store", store, "apply", rid, "--to", str(dest)], capsys)
assert code == 2
assert "write allowlist" in out.err
assert (dest / "alpha" / "SKILL.md").read_text(encoding="utf-8") == before
assert not home.exists()


def test_show_still_reports_a_text_plan_inside_the_allowlist(
tmp_path, capsys, monkeypatch):
# A text artifact backs up to a sibling `.bak-N`, inside the allowlist when
# the destination is: that plan stays green, unchanged.
store = str(tmp_path / "runs")
target = tmp_path / "allowed" / "prompt.txt"
target.parent.mkdir()
target.write_text("edited since the run\n", encoding="utf-8")
rd = runstore.create({
"kind": "text", "target": str(target),
"data": {"inline": [{"prompt": "p", "gold": "g"}],
"prompt": "prompt", "gold": "gold"},
"score": "exact", "agent": {"ref": "echo"}, "reflect": {"ref": "echo"},
}, store=store)
EvolutionResult(state={}, rendered="new text\n", final_reward=1.0,
history=[], ledger_log=[]).save(rd.result_path)
rd.update_status(state="done")
monkeypatch.setenv("AGENTDESCENT_SURFACE_WRITE_DIRS", str(target.parent))

code, out = _run_cli(["--store", store, "show", str(rd.run_id), "--diff",
"--json"], capsys)
assert code == 0
plan = json.loads(out.out)["apply_plan"]
assert plan.get("error") is None
assert plan["written"] == [str(target)]

code, out = _run_cli(["--store", store, "apply", str(rd.run_id),
"--to", str(target)], capsys)
assert code == 0
assert target.read_text(encoding="utf-8") == "new text\n"
assert (target.parent / "prompt.txt.bak-0").read_text(
encoding="utf-8") == "edited since the run\n"


def test_show_keeps_allow_anywhere_when_the_allowlist_is_unset(
tmp_path, capsys, monkeypatch):
# Unset outside a worker is not "refuse everything": the plan is still shown.
store, rid = _make_library_run(tmp_path)
monkeypatch.delenv("AGENTDESCENT_SURFACE_WRITE_DIRS", raising=False)
monkeypatch.delenv(NESTED_ENV, raising=False)

code, out = _run_cli(["--store", store, "show", rid, "--diff", "--json"], capsys)
assert code == 0
plan = json.loads(out.out)["apply_plan"]
assert plan.get("error") is None
assert plan["written"]
Loading