From 8e983aa832c9b5332ef9bc727b205a221c2c61e0 Mon Sep 17 00:00:00 2001 From: Danyang Chen Date: Wed, 18 Nov 2026 06:23:27 +0800 Subject: [PATCH] fix(cli): snapshot apply writes a text candidate's prompt Closes #1149 --- agentdescent/cli.py | 21 +++++++-- changelog.d/1149-snapshot-apply-text.md | 2 + tests/test_snapshot_cli.py | 60 ++++++++++++++++++++++++- 3 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 changelog.d/1149-snapshot-apply-text.md diff --git a/agentdescent/cli.py b/agentdescent/cli.py index 41aba6f1..9ea22c4c 100644 --- a/agentdescent/cli.py +++ b/agentdescent/cli.py @@ -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()) diff --git a/changelog.d/1149-snapshot-apply-text.md b/changelog.d/1149-snapshot-apply-text.md new file mode 100644 index 00000000..be9fc08e --- /dev/null +++ b/changelog.d/1149-snapshot-apply-text.md @@ -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). diff --git a/tests/test_snapshot_cli.py b/tests/test_snapshot_cli.py index 0ea4bfec..3710e459 100644 --- a/tests/test_snapshot_cli.py +++ b/tests/test_snapshot_cli.py @@ -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 @@ -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"