The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(update): pack tidy never caches a partial comparison, and clears MIDX layers on Windows
Teknium7157422022ff
diff --git a/hermes_cli/git_pack_tidy.py b/hermes_cli/git_pack_tidy.pyindex 119e1589f2e..3a5d0aed8f4 100644--- a/hermes_cli/git_pack_tidy.py+++ b/hermes_cli/git_pack_tidy.py@@ -18,6 +18,7 @@ Each erase and each merge is atomic for git (a pack exists for git only while bofrom __future__ import annotations+import contextlibimport jsonimport loggingimport mmap@@ -48,6 +49,7 @@ _MERGE_BATCH_BYTES = 256 * 1024 * 1024# since removing packs never adds copies) and the merge batch size, halved after a merge that ran out# of time. Remembering both is what guarantees progress: no update repeats work an earlier one finished._STATE_FILE = "hermes-pack-tidy.json"+_STATE_VERSION = 2 # v1 could record a pack as unique after comparing it with only some packs# Two tidies at once could each erase a pack because the other still has its copies._LOCK_FILE = "hermes-pack-tidy.lock"# Payload first: git loads a pack only when both .idx and .pack exist, so once .pack is gone the pack is@@ -132,7 +134,13 @@ def _drop_midx(pack_dir: Path) -> None:(pack_dir / "multi-pack-index").unlink(missing_ok=True)layers = pack_dir / "multi-pack-index.d"(layers / "multi-pack-index-chain").unlink(missing_ok=True)- shutil.rmtree(layers, ignore_errors=True)+ for layer in layers.glob("*"):+ with contextlib.suppress(OSError): # a layer a reader still maps goes on a later run+ if os.name == "nt":+ os.chmod(layer, 0o666) # git writes layers read-only; Windows refuses to unlink those+ layer.unlink()+ with contextlib.suppress(OSError):+ layers.rmdir()def _remove_pack(pack: Path) -> int:@@ -170,6 +178,8 @@ def _sweep_remnants(pack_dir: Path) -> None:def _load_state(pack_dir: Path) -> dict:try:state = json.loads((pack_dir.parent.parent / _STATE_FILE).read_text(encoding="utf-8-sig"))+ if state.get("v") != _STATE_VERSION:+ raise ValueError("pack tidy state from another version")kept, seen = set(state.get("kept", [])), set(state.get("seen", []))merge_bytes = int(state.get("merge_bytes", _MERGE_BATCH_BYTES))except (OSError, ValueError, TypeError, AttributeError):@@ -181,7 +191,8 @@ def _load_state(pack_dir: Path) -> dict:def _save_state(pack_dir: Path, state: dict) -> None:live = sorted(p.stem for p in pack_dir.glob("pack-*.pack"))- data = {"kept": sorted(state["kept"] & set(live)), "seen": live, "merge_bytes": state["merge_bytes"]}+ data = {"v": _STATE_VERSION, "kept": sorted(state["kept"] & set(live)), "seen": live,+ "merge_bytes": state["merge_bytes"]}try:(pack_dir.parent.parent / _STATE_FILE).write_text(json.dumps(data), encoding="utf-8")except OSError:@@ -226,6 +237,7 @@ def _erase_redundant_packs(pack_dir: Path, deadline: float, result: TidyResult,indexes[pack.stem] = _Index(pack.with_suffix(".idx"))except (OSError, ValueError): # unreadable now: neither a candidate nor a copy, retried next runlogger.debug("pack index %s unreadable", pack.name, exc_info=True)+ every_index_read = len(indexes) == len(sizes)by_size = sorted(sizes, key=lambda p: sizes[p], reverse=True) # big packs hold most copiesfor pack in sorted(candidates, key=lambda p: sizes[p]): # small first: cheapest to prove, likeliest copiesif pack.stem not in indexes:@@ -236,7 +248,8 @@ def _erase_redundant_packs(pack_dir: Path, deadline: float, result: TidyResult,result.out_of_time = Truereturnif not verdict:- state["kept"].add(pack.stem)+ if every_index_read: # unique among ALL packs; with one unread it is only unknown+ state["kept"].add(pack.stem)continueindexes.pop(pack.stem).close()try:diff --git a/tests/hermes_cli/test_git_pack_tidy.py b/tests/hermes_cli/test_git_pack_tidy.pyindex 0b6a617eb8f..c3c0ab1c2c2 100644--- a/tests/hermes_cli/test_git_pack_tidy.py+++ b/tests/hermes_cli/test_git_pack_tidy.py@@ -83,9 +83,17 @@ def test_erases_only_packs_whose_every_object_has_another_copy(clone: Path, monkclone_pack.with_suffix(".keep").write_text("pinned by hand\n", encoding="utf-8")_age(clone)held = _objects(clone)- with monkeypatch.context() as patched: # an index that cannot be read this time is retried next time- patched.setattr(tidy._Index, "__init__", lambda self, idx: (_ for _ in ()).throw(OSError("busy")))- assert tidy.tidy_partial_clone_packs(clone).erased == 0+ biggest = max(before, key=lambda p: p.stat().st_size).with_suffix(".idx") # where most copies live+ real_init = tidy._Index.__init__++ def busy_biggest(self: object, idx: Path) -> None:+ if idx == biggest:+ raise OSError("busy")+ real_init(self, idx)++ with monkeypatch.context() as patched: # without the index holding the copies, nothing is proven unique+ patched.setattr(tidy._Index, "__init__", busy_biggest)+ tidy.tidy_partial_clone_packs(clone)result = tidy.tidy_partial_clone_packs(clone)