@@ -280,6 +280,54 @@ def boom(**kwargs: object) -> str:
280280 assert store .resolve_local_path ("tiny" ) == ckpt
281281
282282
283+ def test_failed_swap_restores_backup_checkpoint (
284+ tmp_path : Path ,
285+ monkeypatch : pytest .MonkeyPatch ,
286+ ) -> None :
287+ """Failure after the old checkpoint moved to backup must roll it back."""
288+ store = ModelStore (cache_root = tmp_path )
289+ ckpt = tmp_path / "tiny" / "checkpoint"
290+ _write_checkpoint (ckpt , config = '{"model": "keep"}' )
291+ digest = _config_digest (ckpt )
292+ store .write_manifest (
293+ ModelManifest (
294+ tier = "tiny" ,
295+ repo_id = "Unplug-AI/test" ,
296+ revision = load_catalog ().tiers ["tiny" ].revision ,
297+ path = str (ckpt ),
298+ config_digest = digest ,
299+ )
300+ )
301+
302+ def fake_snapshot_download (** kwargs : object ) -> str :
303+ local_dir = Path (str (kwargs ["local_dir" ]))
304+ _write_checkpoint (local_dir , config = '{"model": "new"}' )
305+ return str (local_dir )
306+
307+ hf = ModuleType ("huggingface_hub" )
308+ hf .snapshot_download = fake_snapshot_download # type: ignore[attr-defined]
309+ monkeypatch .setitem (sys .modules , "huggingface_hub" , hf )
310+ monkeypatch .setattr (
311+ "unplug.ml.store.require_huggingface_hub" ,
312+ lambda : None ,
313+ )
314+
315+ def broken_write (manifest : ModelManifest ) -> None :
316+ msg = "disk full"
317+ raise OSError (msg )
318+
319+ monkeypatch .setattr (store , "write_manifest" , broken_write )
320+
321+ with pytest .raises (OSError , match = "disk full" ):
322+ store .ensure_tier ("tiny" , force = True )
323+
324+ # Old checkpoint restored at the original path; manifest still matches it.
325+ assert (ckpt / "config.json" ).read_text (encoding = "utf-8" ) == '{"model": "keep"}'
326+ assert store .resolve_local_path ("tiny" ) == ckpt
327+ leftovers = [p .name for p in (tmp_path / "tiny" ).iterdir () if p .name != "checkpoint" ]
328+ assert leftovers == ["manifest.json" ]
329+
330+
283331def test_invalid_unplug_model_path_logs_warning (
284332 tmp_path : Path ,
285333 monkeypatch : pytest .MonkeyPatch ,
0 commit comments