Open
feat: #8 --force 覆盖改为 staging+rename 原子替换,保留 data/ 运行数据 #1
grape_botAdd on Oct 20
feat: #8 --force 覆盖改为 staging+rename 原子替换,保留 data/ 运行数据 #1
Open
Merged fromfeat/issue-8intomain
Total: 5 files changed+319-11
| @@ -37,7 +37,7 @@ SKILLS = [ | |||
| 37 | "real-data-sync", | 37 | "real-data-sync", |
| 38 | ] | 38 | ] |
| 39 | 39 | ||
| 40 | -SKIP_DIRS = {"__pycache__", ".git", "tmp_pipeline", "tmp", "output", "rfm_result"} | 40 | +SKIP_DIRS = {"__pycache__", ".git", "tmp_pipeline", "tmp", "output", "rfm_result", "data"} |
| 41 | 41 | ||
| 42 | if sys.stdout and hasattr(sys.stdout, "reconfigure"): | 42 | if sys.stdout and hasattr(sys.stdout, "reconfigure"): |
| 43 | try: | 43 | try: |
| @@ -65,25 +65,87 @@ def find_workspace(cli_value): | |||
| 65 | return None | 65 | return None |
| 66 | 66 | ||
| 67 | 67 | ||
| 68 | +def _count_data_files(dst): | ||
| 69 | + """统计已部署技能目录下 data/ 运行数据文件数(无 data 目录则为 0)。""" | ||
| 70 | + data_dir = os.path.join(dst, "data") | ||
| 71 | + if not os.path.isdir(data_dir): | ||
| 72 | + return 0 | ||
| 73 | + return sum(len(files) for _, _, files in os.walk(data_dir)) | ||
| 74 | + | ||
| 75 | + | ||
| 76 | +def _atomic_replace(src, dst, staging, backup, name): | ||
| 77 | + """staging 复制 → 校验 → rename 原子替换(备份可回滚)。 | ||
| 78 | + | ||
| 79 | + 步骤(staging 与 dst 同级 → 同分区,rename 原子): | ||
| 80 | + 1. copytree(src → staging, ignore=SKIP_DIRS) | ||
| 81 | + 2. 旧目录存在 data/ → rename 到 staging(保留运行期数据) | ||
| 82 | + 3. rename(dst → backup) 备份旧目录 | ||
| 83 | + 4. rename(staging → dst) 原子替换 | ||
| 84 | + 5. rmtree(backup) 成功后清理备份 | ||
| 85 | + 任一步异常:自动回滚 backup → dst,清理 staging,返回错误信息。 | ||
| 86 | + """ | ||
| 87 | + try: | ||
| 88 | + # 清理上次失败可能残留的 staging | ||
| 89 | + if os.path.exists(staging): | ||
| 90 | + shutil.rmtree(staging) | ||
| 91 | + shutil.copytree(src, staging, ignore=shutil.ignore_patterns(*SKIP_DIRS)) | ||
| 92 | + # 校验 staging:SKILL.md 必须存在 | ||
| 93 | + if not os.path.exists(os.path.join(staging, "SKILL.md")): | ||
| 94 | + raise OSError(f"staging 校验失败: {staging} 缺少 SKILL.md") | ||
| 95 | + # 保留旧目录的运行期 data/(SKIP_DIRS 已跳过复制,这里整体迁移) | ||
| 96 | + old_data = os.path.join(dst, "data") | ||
| 97 | + if os.path.isdir(old_data): | ||
| 98 | + os.rename(old_data, os.path.join(staging, "data")) | ||
| 99 | + os.rename(dst, backup) | ||
| 100 | + os.rename(staging, dst) | ||
| 101 | + try: | ||
| 102 | + shutil.rmtree(backup) | ||
| 103 | + except Exception as e: | ||
| 104 | + # 替换已成功,仅备份清理失败:不视为部署失败,提示手动清理 | ||
| 105 | + print(f"[WARN] {name} 原子替换成功,但旧备份清理失败: {e}" | ||
| 106 | + f"(可手动删除 {backup})") | ||
| 107 | + return None | ||
| 108 | + except Exception as e: | ||
| 109 | + # 兜底回滚:backup 存在且 dst 缺失 → 恢复旧目录 | ||
| 110 | + try: | ||
| 111 | + if os.path.exists(backup) and not os.path.exists(dst): | ||
| 112 | + os.rename(backup, dst) | ||
| 113 | + # data 已迁入 staging 时搬回旧目录(避免随 staging 一起被清理) | ||
| 114 | + staging_data = os.path.join(staging, "data") | ||
| 115 | + if os.path.isdir(staging_data) and not os.path.exists(os.path.join(dst, "data")): | ||
| 116 | + os.rename(staging_data, os.path.join(dst, "data")) | ||
| 117 | + if os.path.exists(staging): | ||
| 118 | + shutil.rmtree(staging) | ||
| 119 | + except Exception as rollback_err: | ||
| 120 | + return (f"[FAIL] {name} 原子替换失败: {e};且回滚异常: {rollback_err}" | ||
| 121 | + f"(备份保留于 {backup},可手动 rename 恢复)") | ||
| 122 | + return f"[FAIL] {name} 覆盖失败(已回滚,旧目录完整): {e}" | ||
| 123 | + | ||
| 124 | + | ||
| 68 | def copy_skill(name, skills_dir, force, dry_run): | 125 | def copy_skill(name, skills_dir, force, dry_run): |
| 69 | src = os.path.join(ROOT, name) | 126 | src = os.path.join(ROOT, name) |
| 70 | dst = os.path.join(skills_dir, name) | 127 | dst = os.path.join(skills_dir, name) |
| 128 | + staging = dst + ".new" | ||
| 129 | + backup = dst + ".bak" | ||
| 71 | if not os.path.isdir(src): | 130 | if not os.path.isdir(src): |
| 72 | return f"[FAIL] 源技能不存在: {src}" | 131 | return f"[FAIL] 源技能不存在: {src}" |
| 73 | if os.path.exists(dst): | 132 | if os.path.exists(dst): |
| 74 | if not force: | 133 | if not force: |
| 75 | return (f"[SKIP] 已存在: {dst}\n" | 134 | return (f"[SKIP] 已存在: {dst}\n" |
| 76 | - f" 如需覆盖请加 --force(会先删除旧目录)") | 135 | + f" 如需覆盖请加 --force(staging 复制 + rename 原子替换,可回滚)") |
| 77 | - if not dry_run: | 136 | + if dry_run: |
| 78 | - shutil.rmtree(dst) | 137 | + return (f"[DRY-RUN] 将原子覆盖: {dst}" |
| 79 | - note = "(已覆盖旧目录)" | 138 | + f"(data/ 运行数据文件 {_count_data_files(dst)} 个,将保留)") |
| 80 | - else: | 139 | + print(f"[..] 覆盖目标: {dst}(data/ 运行数据文件 {_count_data_files(dst)} 个,将保留)") |
| 81 | - note = "" | 140 | + err = _atomic_replace(src, dst, staging, backup, name) |
| 141 | + if err: | ||
| 142 | + return err | ||
| 143 | + return f"[OK] {name} → {dst} (原子替换完成,旧目录已备份并清理)" | ||
| 82 | if not dry_run: | 144 | if not dry_run: |
| 83 | shutil.copytree(src, dst, | 145 | shutil.copytree(src, dst, |
| 84 | ignore=shutil.ignore_patterns(*SKIP_DIRS), | 146 | ignore=shutil.ignore_patterns(*SKIP_DIRS), |
| 85 | dirs_exist_ok=True) | 147 | dirs_exist_ok=True) |
| 86 | - return f"[OK] {name} → {dst} {note}" | 148 | + return f"[OK] {name} → {dst} " |
| 87 | 149 | ||
| 88 | 150 | ||
| 89 | def verify(skills_dir): | 151 | def verify(skills_dir): |
| @@ -146,12 +208,12 @@ def resolve_skills_dir(ws): | |||
| 146 | return candidate | 208 | return candidate |
| 147 | 209 | ||
| 148 | 210 | ||
| 149 | -def main(): | 211 | +def main(argv=None): |
| 150 | parser = argparse.ArgumentParser(description="OpsSwarm → JiuwenClaw 部署") | 212 | parser = argparse.ArgumentParser(description="OpsSwarm → JiuwenClaw 部署") |
| 151 | parser.add_argument("--workspace", default=None, help="JiuwenClaw workspace 路径(可选,默认自动探测)") | 213 | parser.add_argument("--workspace", default=None, help="JiuwenClaw workspace 路径(可选,默认自动探测)") |
| 152 | parser.add_argument("--dry-run", action="store_true", help="只显示将执行的操作,不实际复制") | 214 | parser.add_argument("--dry-run", action="store_true", help="只显示将执行的操作,不实际复制") |
| 153 | - parser.add_argument("--force", action="store_true", help="同名技能已存在时强制覆盖") | 215 | + parser.add_argument("--force", action="store_true", help="同名技能已存在时强制覆盖(staging + rename 原子替换,可回滚)") |
| 154 | - args = parser.parse_args() | 216 | + args = parser.parse_args(argv) |
| 155 | 217 | ||
| 156 | print("=" * 64) | 218 | print("=" * 64) |
| 157 | print("OpsSwarm → JiuwenClaw 本地部署") | 219 | print("OpsSwarm → JiuwenClaw 本地部署") |
| @@ -0,0 +1,56 @@ | |||
| 1 | +# Issue #8 方案设计(plan) | ||
| 2 | + | ||
| 3 | +## 技术方案 | ||
| 4 | + | ||
| 5 | +**staging + rename 原子替换**(同分区保证:staging 与 dst 同在 skills 目录下)。 | ||
| 6 | + | ||
| 7 | +### copy_skill() force 分支新流程 | ||
| 8 | + | ||
| 9 | +``` | ||
| 10 | +dst 已存在且 force: | ||
| 11 | + 1. 打印覆盖提示(目标目录 + data/ 运行数据文件数) | ||
| 12 | + 2. 清理残留 staging:dst.new(若存在则 rmtree) | ||
| 13 | + 3. copytree(src → dst.new, ignore=SKIP_DIRS∪{data}) | ||
| 14 | + 4. 若旧 dst 存在 data/ → rename(dst/data → dst.new/data) # data 保留 | ||
| 15 | + 5. rename(dst → dst.bak) # 备份 | ||
| 16 | + 6. rename(dst.new → dst) # 原子替换 | ||
| 17 | + 7. rmtree(dst.bak) # 成功清理备份 | ||
| 18 | +异常(3-6 任一步): | ||
| 19 | + - 若 dst.bak 存在且 dst 缺失 → rename(dst.bak → dst) 回滚 | ||
| 20 | + - 清理 dst.new 残留 | ||
| 21 | + - 返回 [FAIL] 明确错误信息 → main() 以退出码 1 退出 | ||
| 22 | +``` | ||
| 23 | + | ||
| 24 | +- 步骤 4 失败(data 迁移失败):清理 staging,旧目录未动,直接 FAIL。 | ||
| 25 | +- 步骤 5/6 失败:回滚 `.bak` → dst,清理 `.new`,FAIL。 | ||
| 26 | +- 非force 已存在 → 仍为 [SKIP](不变)。 | ||
| 27 | +- dst 不存在 → 直接 copytree 到 dst(无替换风险,保持原样,但仍走 staging? 否——无旧目录无需原子替换,直接复制即可,与现状一致)。 | ||
| 28 | + | ||
| 29 | +### SKIP_DIRS | ||
| 30 | + | ||
| 31 | +`{"__pycache__", ".git", "tmp_pipeline", "tmp", "output", "rfm_result", "data"}` | ||
| 32 | + | ||
| 33 | +### data 文件数提示 | ||
| 34 | + | ||
| 35 | +`count = sum(len(files) for _, _, files in os.walk(dst/data))`,`--force` 且 dst 存在时打印:目标目录 + data 文件数(无 data 打印 0)。 | ||
| 36 | + | ||
| 37 | +### 测试(tests/test_deploy_force.py,pytest + monkeypatch/stdlib) | ||
| 38 | + | ||
| 39 | +1. `test_force_upgrade_keeps_data`:预置 dst 含 data/ 运行文件 + 旧 SKILL.md;跑 main(--force) → data 文件不变、SKILL.md 更新、无 .new/.bak。 | ||
| 40 | +2. `test_copy_failure_rolls_back`:monkeypatch `shutil.copytree` 抛异常 → main 退出码非 0,旧目录内容完整、无 .new/.bak。 | ||
| 41 | +3. `test_rename_failure_rolls_back`:monkeypatch `os.rename` 在备份阶段后抛异常 → 回滚后旧目录完整。 | ||
| 42 | +4. `test_dry_run_no_changes`:预置 dst;跑 main(--dry-run --force) → 目录内容/时间戳不变、无 .new/.bak。 | ||
| 43 | +5. `test_skip_dirs_contains_data`:断言 `"data" in SKIP_DIRS`。 | ||
| 44 | + | ||
| 45 | +通过 `subprocess` 或直接 import 模块 + monkeypatch 调 `main()` 并捕获 SystemExit;使用 tmp_path 构造假 workspace(`agent/skills` 布局)与假 ROOT(monkeypatch deploy 模块的 ROOT)。 | ||
| 46 | + | ||
| 47 | +## 受影响模块 | ||
| 48 | + | ||
| 49 | +- `deploy/jiuwenclaw_deploy.py`(修改) | ||
| 50 | +- `tests/test_deploy_force.py`(新增) | ||
| 51 | +- `docs/issue-8/spec.md` / `plan.md` / `task.md`(文档) | ||
| 52 | + | ||
| 53 | +## 回滚与风险 | ||
| 54 | + | ||
| 55 | +- 失败路径全部自动回滚 `.bak`;最坏情况(回滚也失败)保留 `.bak` 目录并明确报错,人工可手工 rename 恢复。 | ||
| 56 | +- dry-run 不做任何写操作(含 staging)。 | ||
| @@ -0,0 +1,42 @@ | |||
| 1 | +# Issue #8 需求文档(spec) | ||
| 2 | + | ||
| 3 | +## 背景问题 | ||
| 4 | + | ||
| 5 | +`deploy/jiuwenclaw_deploy.py --force` 当前覆盖逻辑为「先 `shutil.rmtree(dst)` 再 `copytree`」: | ||
| 6 | + | ||
| 7 | +1. **中途失败 = 技能丢失**:rmtree 成功后若 copytree 中途失败(磁盘满、权限、源被并发修改等),目标目录处于「半成品 / 空」状态,旧版本技能已无法恢复。 | ||
| 8 | +2. **运行期数据被清掉**:`real-data-sync` 技能在部署机上会在自身目录下产生运行期数据 `data/`,rmtree 会把它一并删除,导致线上运行数据丢失。 | ||
| 9 | + | ||
| 10 | +## 目标 | ||
| 11 | + | ||
| 12 | +- `--force` 覆盖改为 **staging 复制 → 校验 → rename 原子替换(备份可回滚)** 策略: | ||
| 13 | + 1. `copytree` 到 `dst + ".new"`(staging,与 dst 同级 → 同分区,保证 rename 原子性); | ||
| 14 | + 2. 旧目录 rename 为 `dst + ".bak"`(备份,可回滚); | ||
| 15 | + 3. `.new` rename 为 `dst`(原子替换); | ||
| 16 | + 4. 成功后清理 `.bak`; | ||
| 17 | + 5. 任一步异常 → 自动回滚 `.bak` 为 `dst`,清理 `.new`,打印明确错误并以非 0 退出码退出。 | ||
| 18 | +- `SKIP_DIRS`(deploy/jiuwenclaw_deploy.py:39)增加 `"data"`:复制阶段跳过 `data/`,即已部署技能目录里的运行期数据目录不会被 staging 复制带走/影响,同时 `real-data-sync/data` 在升级后保持不变(data 不参与复制,原目录 rename 保留)。 | ||
| 19 | +- `--force` 覆盖前打印提示:目标目录 + `data/` 运行数据文件数。 | ||
| 20 | +- 新增 `tests/test_deploy_force.py`:失败注入回滚 / data 保留 / dry-run 零改动。 | ||
| 21 | + | ||
| 22 | +## 范围(In) | ||
| 23 | + | ||
| 24 | +- `deploy/jiuwenclaw_deploy.py`:`copy_skill()` 重写、`SKIP_DIRS` 增加 `data`、覆盖前提示、非 0 退出码。 | ||
| 25 | +- `tests/test_deploy_force.py`(新增)。 | ||
| 26 | + | ||
| 27 | +## 非目标(Out) | ||
| 28 | + | ||
| 29 | +- 不增加 `--keep-data` 开关(data 默认保留)。 | ||
| 30 | +- 不修改 `verify()`、workspace 探测等其它逻辑。 | ||
| 31 | + | ||
| 32 | +## 验收标准 | ||
| 33 | + | ||
| 34 | +1. 注入复制失败后,旧技能目录完整(自动回滚成功),无 `.new`/`.bak` 残留、无半成品状态。 | ||
| 35 | +2. 正常 `--force` 升级后,`real-data-sync/data` 下文件保持不变。 | ||
| 36 | +3. 成功后无 `.new`/`.bak` 残留;失败输出明确错误且退出码非 0。 | ||
| 37 | +4. `python -m pytest tests/` 全绿(含现有 `test_skills.py` / `test_pipeline.py`)。 | ||
| 38 | + | ||
| 39 | +## 关键设计点 | ||
| 40 | + | ||
| 41 | +- **同分区保证**:staging 目录 `dst.new` 放在 dst 同级(同一 skills 目录下),`os.rename` 同分区才是原子操作。 | ||
| 42 | +- **data 保留机制**:`data` 进 `SKIP_DIRS` 后 staging 里不含 data;rename 替换后新目录无 data,因此需要在替换后把旧目录(`.bak`)中的 `data/` 迁移回新目录,或在 SKIP 前先迁移 —— 方案选「替换前从旧目录整体搬移 `data/` 到 staging」:若旧目录存在 `data/`,复制完成后将 `old/data` rename 到 `staging/data`,再执行替换;失败回滚时 `data/` 仍在 `.bak` 中随回滚一起恢复。 | ||
| @@ -0,0 +1,12 @@ | |||
| 1 | +# Issue #8 任务拆解(task) | ||
| 2 | + | ||
| 3 | +| # | 任务 | 产出 | 状态 | | ||
| 4 | +|---|------|------|------| | ||
| 5 | +| 1 | 编写需求文档 | docs/issue-8/spec.md | ✅ | | ||
| 6 | +| 2 | 编写方案设计 | docs/issue-8/plan.md | ✅ | | ||
| 7 | +| 3 | TDD:先写测试 tests/test_deploy_force.py(RED) | tests/test_deploy_force.py | ✅ | | ||
| 8 | +| 4 | 实现 copy_skill 原子替换 + SKIP_DIRS + 提示(GREEN) | deploy/jiuwenclaw_deploy.py | ✅ | | ||
| 9 | +| 5 | 全量测试 pytest tests/ 全绿 | 8 passed + test_skills 54 + test_pipeline 50 | ✅ | | ||
| 10 | +| 6 | 自审查(5 轴) | 审查通过 | ✅ | | ||
| 11 | +| 7 | 原子提交 + push feat/issue-8 | git | ☐ | | ||
| 12 | +| 8 | 建 PR(base=main)+ Issue 评论 + A2A QA | PR | ☐ | | ||
| @@ -0,0 +1,136 @@ | |||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +"""Issue #8:deploy --force 原子替换与 data 保留测试。 | ||
| 3 | + | ||
| 4 | +覆盖: | ||
| 5 | +- 失败注入(copytree / os.rename)→ 自动回滚 .bak,旧技能目录完整,无 .new/.bak 残留 | ||
| 6 | +- 正常 --force 升级 → real-data-sync/data 运行数据保持不变 | ||
| 7 | +- dry-run → 零改动(无 .new/.bak,目标目录内容不变) | ||
| 8 | +- SKIP_DIRS 含 data | ||
| 9 | +""" | ||
| 10 | + | ||
| 11 | +import os | ||
| 12 | +import shutil | ||
| 13 | +import sys | ||
| 14 | + | ||
| 15 | +import pytest | ||
| 16 | + | ||
| 17 | +DEPLOY_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "deploy") | ||
| 18 | +sys.path.insert(0, DEPLOY_DIR) | ||
| 19 | + | ||
| 20 | +import jiuwenclaw_deploy as deploy # noqa: E402 | ||
| 21 | + | ||
| 22 | + | ||
| 23 | +def _make_skill_root(tmp_path, skill_name="real-data-sync", body="v1"): | ||
| 24 | + """构造假源仓库 ROOT:含一个技能目录 + deploy/ 占位。""" | ||
| 25 | + root = tmp_path / "repo" | ||
| 26 | + (root / skill_name).mkdir(parents=True) | ||
| 27 | + (root / skill_name / "SKILL.md").write_text(f"---\nname: {skill_name}\n---\n{body}\n", encoding="utf-8") | ||
| 28 | + (root / "shared").mkdir() | ||
| 29 | + return root | ||
| 30 | + | ||
| 31 | + | ||
| 32 | +def _make_workspace(tmp_path, skill_name="real-data-sync", installed_body="v0", data_files=None): | ||
| 33 | + """构造假 JiuwenClaw workspace:<ws>/agent/skills/<skill>(含旧版本 + 运行期 data/)。""" | ||
| 34 | + skills_dir = tmp_path / "ws" / "agent" / "skills" | ||
| 35 | + dst = skills_dir / skill_name | ||
| 36 | + dst.mkdir(parents=True) | ||
| 37 | + (dst / "SKILL.md").write_text(f"---\nname: {skill_name}\n---\n{installed_body}\n", encoding="utf-8") | ||
| 38 | + if data_files: | ||
| 39 | + data_dir = dst / "data" | ||
| 40 | + data_dir.mkdir() | ||
| 41 | + for rel, content in data_files.items(): | ||
| 42 | + p = data_dir / rel | ||
| 43 | + p.parent.mkdir(parents=True, exist_ok=True) | ||
| 44 | + p.write_text(content, encoding="utf-8") | ||
| 45 | + return skills_dir | ||
| 46 | + | ||
| 47 | + | ||
| 48 | +def _no_residue(skills_dir, skill_name): | ||
| 49 | + assert not os.path.exists(os.path.join(str(skills_dir), skill_name + ".new")), ".new 残留" | ||
| 50 | + assert not os.path.exists(os.path.join(str(skills_dir), skill_name + ".bak")), ".bak 残留" | ||
| 51 | + | ||
| 52 | + | ||
| 53 | +def test_skip_dirs_contains_data(): | ||
| 54 | + assert "data" in deploy.SKIP_DIRS | ||
| 55 | + | ||
| 56 | + | ||
| 57 | +def test_force_upgrade_keeps_data(tmp_path, monkeypatch): | ||
| 58 | + root = _make_skill_root(tmp_path, body="v2") | ||
| 59 | + data_files = {"run1.json": '{"n":1}', "sub/run2.csv": "a,b\n1,2\n"} | ||
| 60 | + skills_dir = _make_workspace(tmp_path, installed_body="v0", data_files=data_files) | ||
| 61 | + monkeypatch.setattr(deploy, "ROOT", str(root)) | ||
| 62 | + monkeypatch.setattr(deploy, "SKILLS", ["real-data-sync"]) | ||
| 63 | + # 假 workspace 不含完整技能集,stub 掉部署验证(本测试聚焦 data 保留) | ||
| 64 | + monkeypatch.setattr(deploy, "verify", lambda ws: ([], [])) | ||
| 65 | + deploy.main(["--workspace", str(tmp_path / "ws"), "--force"]) | ||
| 66 | + dst = skills_dir / "real-data-sync" | ||
| 67 | + # data 运行数据保持不变 | ||
| 68 | + for rel, content in data_files.items(): | ||
| 69 | + assert (dst / "data" / rel).read_text(encoding="utf-8") == content | ||
| 70 | + # 技能本体已升级 | ||
| 71 | + assert "v2" in (dst / "SKILL.md").read_text(encoding="utf-8") | ||
| 72 | + _no_residue(skills_dir, "real-data-sync") | ||
| 73 | + | ||
| 74 | + | ||
| 75 | +def test_copy_failure_rolls_back(tmp_path, monkeypatch): | ||
| 76 | + root = _make_skill_root(tmp_path, body="v2") | ||
| 77 | + data_files = {"run1.json": '{"n":1}'} | ||
| 78 | + skills_dir = _make_workspace(tmp_path, installed_body="v0", data_files=data_files) | ||
| 79 | + monkeypatch.setattr(deploy, "ROOT", str(root)) | ||
| 80 | + monkeypatch.setattr(deploy, "SKILLS", ["real-data-sync"]) | ||
| 81 | + | ||
| 82 | + def boom(*a, **kw): | ||
| 83 | + raise OSError("disk full (injected)") | ||
| 84 | + monkeypatch.setattr(deploy.shutil, "copytree", boom) | ||
| 85 | + | ||
| 86 | + with pytest.raises(SystemExit) as ei: | ||
| 87 | + deploy.main(["--workspace", str(tmp_path / "ws"), "--force"]) | ||
| 88 | + assert ei.value.code == 1 | ||
| 89 | + dst = skills_dir / "real-data-sync" | ||
| 90 | + # 旧技能目录完整(含 data) | ||
| 91 | + assert "v0" in (dst / "SKILL.md").read_text(encoding="utf-8") | ||
| 92 | + assert (dst / "data" / "run1.json").read_text(encoding="utf-8") == '{"n":1}' | ||
| 93 | + _no_residue(skills_dir, "real-data-sync") | ||
| 94 | + | ||
| 95 | + | ||
| 96 | +def test_rename_failure_rolls_back(tmp_path, monkeypatch): | ||
| 97 | + root = _make_skill_root(tmp_path, body="v2") | ||
| 98 | + data_files = {"run1.json": '{"n":1}'} | ||
| 99 | + skills_dir = _make_workspace(tmp_path, installed_body="v0", data_files=data_files) | ||
| 100 | + monkeypatch.setattr(deploy, "ROOT", str(root)) | ||
| 101 | + monkeypatch.setattr(deploy, "SKILLS", ["real-data-sync"]) | ||
| 102 | + | ||
| 103 | + real_rename = os.rename | ||
| 104 | + | ||
| 105 | + def flaky_rename(src, dst): | ||
| 106 | + # 放行 data 迁移与 .bak 备份,在最终替换(.new → dst)时失败 | ||
| 107 | + if str(src).endswith(".new"): | ||
| 108 | + raise OSError("rename failed (injected)") | ||
| 109 | + return real_rename(src, dst) | ||
| 110 | + monkeypatch.setattr(deploy.os, "rename", flaky_rename) | ||
| 111 | + | ||
| 112 | + with pytest.raises(SystemExit) as ei: | ||
| 113 | + deploy.main(["--workspace", str(tmp_path / "ws"), "--force"]) | ||
| 114 | + assert ei.value.code == 1 | ||
| 115 | + dst = skills_dir / "real-data-sync" | ||
| 116 | + assert "v0" in (dst / "SKILL.md").read_text(encoding="utf-8") | ||
| 117 | + assert (dst / "data" / "run1.json").exists() | ||
| 118 | + _no_residue(skills_dir, "real-data-sync") | ||
| 119 | + | ||
| 120 | + | ||
| 121 | +def test_dry_run_no_changes(tmp_path, monkeypatch): | ||
| 122 | + root = _make_skill_root(tmp_path, body="v2") | ||
| 123 | + data_files = {"run1.json": '{"n":1}'} | ||
| 124 | + skills_dir = _make_workspace(tmp_path, installed_body="v0", data_files=data_files) | ||
| 125 | + before = sorted(str(p.relative_to(skills_dir)) for p in skills_dir.rglob("*")) | ||
| 126 | + | ||
| 127 | + monkeypatch.setattr(deploy, "ROOT", str(root)) | ||
| 128 | + monkeypatch.setattr(deploy, "SKILLS", ["real-data-sync"]) | ||
| 129 | + monkeypatch.setattr(deploy, "verify", lambda ws: ([], [])) | ||
| 130 | + deploy.main(["--workspace", str(tmp_path / "ws"), "--dry-run", "--force"]) | ||
| 131 | + | ||
| 132 | + after = sorted(str(p.relative_to(skills_dir)) for p in skills_dir.rglob("*")) | ||
| 133 | + assert before == after | ||
| 134 | + dst = skills_dir / "real-data-sync" | ||
| 135 | + assert "v0" in (dst / "SKILL.md").read_text(encoding="utf-8") | ||
| 136 | + _no_residue(skills_dir, "real-data-sync") | ||