DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
fix(skills): restore installed package after failed update
dajiaohuang6baef43ab2f1
diff --git a/deeptutor/services/skill/service.py b/deeptutor/services/skill/service.pyindex 238e60cd7..18461e2f0 100644--- a/deeptutor/services/skill/service.py+++ b/deeptutor/services/skill/service.py@@ -55,6 +55,7 @@ from pathlib import Pathimport reimport shutilfrom typing import Any+from uuid import uuid4import yaml@@ -694,9 +695,18 @@ class SkillService:staging.mkdir(parents=True)skipped = self._copy_support_tree(source, staging)(staging / "SKILL.md").write_text(adapted, encoding="utf-8")- if target_dir.exists():- shutil.rmtree(target_dir)- staging.rename(target_dir)+ previous = self._root / f".{slug}.previous-{uuid4().hex}"+ had_previous = target_dir.exists()+ if had_previous:+ target_dir.rename(previous)+ try:+ staging.rename(target_dir)+ except BaseException:+ if had_previous:+ previous.rename(target_dir)+ raise+ if had_previous:+ shutil.rmtree(previous)finally:if staging.exists():shutil.rmtree(staging, ignore_errors=True)diff --git a/tests/services/skill/test_install_rollback.py b/tests/services/skill/test_install_rollback.pynew file mode 100644index 000000000..885089c3f--- /dev/null+++ b/tests/services/skill/test_install_rollback.py@@ -0,0 +1,48 @@+"""A failed package replacement must preserve the installed skill."""++from pathlib import Path++import pytest++from deeptutor.services.skill.service import SkillService+++def test_failed_replacement_preserves_package_and_origin(tmp_path: Path, monkeypatch) -> None:+ """Exercise the public installer with a filesystem publication failure."""+ service = SkillService(root=tmp_path / "skills", builtin_root=None)+ old = tmp_path / "old"+ old.mkdir()+ (old / "SKILL.md").write_text(+ "---\nname: demo\ndescription: Old version\n---\nOriginal playbook\n",+ encoding="utf-8",+ )+ (old / "references").mkdir()+ (old / "references" / "guide.md").write_text("Original reference", encoding="utf-8")+ origin = {"hub": "eduhub", "version": "1.0.0"}+ service.install_tree(old, origin=origin)+ target = service.root / "demo"+ original = {p.relative_to(target): p.read_bytes() for p in target.rglob("*") if p.is_file()}+ new = tmp_path / "new"+ new.mkdir()+ (new / "SKILL.md").write_text(+ "---\nname: demo\ndescription: New version\n---\nReplacement playbook\n",+ encoding="utf-8",+ )+ rename = Path.rename++ def fail_publication(path: Path, destination: Path) -> Path:+ if path.name.startswith(".install-") and Path(destination) == target:+ raise PermissionError("publication denied")+ return rename(path, destination)++ monkeypatch.setattr(Path, "rename", fail_publication)+ with pytest.raises(PermissionError, match="publication denied"):+ service.install_tree(new, force=True, origin={"hub": "eduhub", "version": "2.0.0"})++ assert target.is_dir()+ assert {+ p.relative_to(target): p.read_bytes() for p in target.rglob("*") if p.is_file()+ } == original+ assert service.hub_origin("demo") == origin+ assert service.get_detail("demo").description == "Old version"+ assert service.read_skill_file("demo", "references/guide.md") == "Original reference"