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
21 changes: 18 additions & 3 deletions agentdescent/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3849,10 +3849,25 @@ def cmd_snapshot_apply(a: argparse.Namespace) -> int:
snap = _snapshot_from_args(a)
if not a.to:
raise runstore.RunStoreError("snapshot apply needs --to PATH")
dest = os.path.expanduser(a.to)
with tempfile.TemporaryDirectory(prefix="ad-snapshot-apply-") as tmp:
src = os.path.join(tmp, snap.artifact_id or "artifact")
os.makedirs(src, exist_ok=True)
snap.materialize(src)
# A text snapshot's candidate is `rendered`, not the strategy's
# internal rule store in `files` (#1149). Materialising `files`
# wrote hash-named rule files into a directory even when --to
# named a file. Tree kinds keep the materialise path.
if snap.kind == "text":
if os.path.isdir(dest):
raise runstore.RunStoreError(
f"snapshot {snap.snapshot_id} is a text artifact and "
f"{dest!r} is a directory; pass --to FILE to write it "
"to a file")
src = os.path.join(tmp, "artifact")
with open(src, "w", encoding="utf-8", newline="") as fh:
fh.write(snap.render())
else:
src = os.path.join(tmp, snap.artifact_id or "artifact")
os.makedirs(src, exist_ok=True)
snap.materialize(src)
payload = apply_from_payload(
src, to=a.to, dry_run=a.dry_run, backup=not a.no_backup,
check_write=_operator_write_guard())
Expand Down
2 changes: 2 additions & 0 deletions changelog.d/1149-snapshot-apply-text.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed
`snapshot apply` on a text snapshot now writes the rendered prompt to the file named by `--to`, with the same backup, no-op, and parent-creation rules as `apply --from`. It previously materialised the strategy's internal rule store (hash-named files) into a directory, and an empty-rules snapshot reported a no-op instead of writing the seed prompt. A directory `--to` is refused. Tree-kind snapshots are unchanged (#1149).
60 changes: 58 additions & 2 deletions tests/test_snapshot_cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""CLI surface for portable snapshots: show, eval, apply — no search."""

import json
import os
from pathlib import Path

from agentdescent.cli import main
Expand Down Expand Up @@ -59,12 +60,67 @@ def boom(*args, **kwargs):
assert report["n"] == 1
assert report["reward"] == 1.0

# Text apply writes `rendered` as one file (#1149). The previous rows
# pinned a hybrid: kind=text but materialising SKILL.md / tests/n.txt
# into a directory, which no real text run produces.
dest = tmp_path / "missing.txt"
assert main(["--json", "snapshot", "apply", str(source), "--to", str(dest),
"--dry-run"]) == 0
payload = json.loads(capsys.readouterr().out)
assert payload.get("ok") is not False
assert payload["noop"] is False
assert not dest.exists()
assert main(["--json", "snapshot", "apply", str(source), "--to", str(dest)]) == 0
payload = json.loads(capsys.readouterr().out)
assert payload["noop"] is False
assert payload["written"] == [os.path.abspath(str(dest))]
assert dest.read_text(encoding="utf-8") == "always say yes"

assert main(["--json", "snapshot", "apply", str(source), "--to", str(dest)]) == 0
payload = json.loads(capsys.readouterr().out)
assert payload["noop"] is True
assert payload["written"] == []
assert dest.read_text(encoding="utf-8") == "always say yes"

as_dir = tmp_path / "as-dir"
as_dir.mkdir()
rc = main(["--json", "snapshot", "apply", str(source), "--to", str(as_dir)])
err = json.loads(capsys.readouterr().out)
assert rc == 2 and err["ok"] is False
assert "is a directory" in err["error"] and "--to FILE" in err["error"]
assert list(as_dir.iterdir()) == []

# An empty-rules text snapshot still applies the seed prompt (render()),
# not a no-op with written: [].
empty = ArtifactSnapshot(
artifact_id="seed.txt", files={}, editable=["**"], frozen=[],
kind="text", version=1, id="seed.txt@v1",
rendered="You are a terse assistant.\n")
empty_src = tmp_path / "empty.json"
empty.save(empty_src)
seed_dest = tmp_path / "prompt.txt"
assert main(["--json", "snapshot", "apply", str(empty_src),
"--to", str(seed_dest)]) == 0
payload = json.loads(capsys.readouterr().out)
assert payload["noop"] is False
assert payload["written"] == [os.path.abspath(str(seed_dest))]
assert seed_dest.read_text(encoding="utf-8") == "You are a terse assistant.\n"

# Tree kinds keep today's materialise behaviour.
tree = ArtifactSnapshot.from_filetree(
FileTree({"SKILL.md": "always say yes\n", "tests/n.txt": "frozen\n"},
frozen=["tests/**"]),
artifact_id="skill", kind="skill_dir",
lineage={"version": 2, "id": "skill@v2", "parent": "skill@v1",
"root": "skill@v1"})
tree_source = tmp_path / "skill-tree.json"
tree.save(tree_source)
target = tmp_path / "installed"
assert main(["--json", "snapshot", "apply", str(source), "--to", str(target),
assert main(["--json", "snapshot", "apply", str(tree_source), "--to", str(target),
"--dry-run"]) == 0
payload = json.loads(capsys.readouterr().out)
assert payload.get("ok") is not False
assert not target.exists() or not any(target.iterdir())
assert main(["--json", "snapshot", "apply", str(source), "--to", str(target)]) == 0
assert main(["--json", "snapshot", "apply", str(tree_source), "--to", str(target)]) == 0
assert (target / "SKILL.md").read_text() == "always say yes\n"
assert (target / "tests/n.txt").read_text() == "frozen\n"
Loading