The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(skill_ledger): blob GC keeps unreferenced blobs younger than an hour
kshitijk4poored6b55d1d877
diff --git a/tests/tools/test_skill_ledger.py b/tests/tools/test_skill_ledger.pyindex 21675c0a696..d9eb99ef33c 100644--- a/tests/tools/test_skill_ledger.py+++ b/tests/tools/test_skill_ledger.py@@ -11,6 +11,7 @@ skill history), reshaped for the all-actor JSONL ledger design.import hashlibimport jsonimport os+import timefrom pathlib import Pathimport pytest@@ -758,6 +759,10 @@ def test_concurrent_appends_never_lose_a_middle_row(ledger_env, monkeypatch):for _ in range(seeds):skill_ledger.append_entry("edit", "my-skill", before=[{"path": "s", "sha256": "0" * 64}],after=[{"path": "s", "sha256": "1" * 64}], evidence={"pad": "x" * 500})+ # Unreferenced blobs: a fresh one is another process's in-flight capture (row not appended yet)+ # and must survive every sweep's blob GC; one older than the grace window is garbage and goes.+ fresh, aged = skill_ledger._store_blob(b"in-flight"), skill_ledger._store_blob(b"stale orphan")+ os.utime(skill_ledger.blobs_dir() / aged, (time.time() - 7200, time.time() - 7200))threads = [threading.Thread(target=writer, args=(k,), name=k) for k in ids]for t in threads:t.start()@@ -769,6 +774,7 @@ def test_concurrent_appends_never_lose_a_middle_row(ledger_env, monkeypatch):present = [json.loads(line)["id"] for line inskill_ledger.ledger_path().read_text(encoding="utf-8").splitlines() if line.strip()]assert present, "the newest row always survives a trim"+ assert (skill_ledger.blobs_dir() / fresh).exists() and not (skill_ledger.blobs_dir() / aged).exists()assert len(present) == seeds + 2 * n - sum(dropped), ("every row is either in the ledger or was counted as trimmed — none silently lost")diff --git a/tools/skill_ledger.py b/tools/skill_ledger.pyindex 402e84b6740..7363b9b6def 100644--- a/tools/skill_ledger.py+++ b/tools/skill_ledger.py@@ -18,6 +18,7 @@ import loggingimport osimport reimport tarfile+import time as _timeimport uuidfrom datetime import datetime, timezonefrom pathlib import Path@@ -36,6 +37,7 @@ _ARCHIVE_TS_SUFFIX_RE = re.compile(r"^(.+)-\d{14}$")_PACKAGE_RESTORE_ACTIONS = frozenset({"delete", "archive", "purge"})_VALID_ACTORS = {"curator", "agent", "user"}_DEFAULT_LEDGER_MAX_BYTES = 5 * 1024 * 1024 # skills.ledger_max_bytes default (5 MB)+_BLOB_GC_GRACE_SECS = 3600 # blobs younger than this may belong to an in-flight capture: never GC them_NON_PACKAGE_TOPS = {".curator_backups", ".hub", ".archive", ".locks"}# Transient/regeneratable local artifacts that must never be swept into a# snapshot, no matter how deep they sit under the skill dir — a stray venv or@@ -451,7 +453,9 @@ def gc_blobs() -> Tuple[int, int]:"""Delete blobs no ledger entry references; returns ``(deleted, bytes_freed)``. The store waswrite-only: on one install 98.9% of 47k blobs (1.18 GB) were unreachable after a venv walk(#107539). Malformed ledger lines, or an unreadable/undecodable ledger, abort the sweep- (blobs are kept) — an entry we cannot read may still hold references."""+ (blobs are kept) — an entry we cannot read may still hold references. Blobs newer than+ ``_BLOB_GC_GRACE_SECS`` are kept too: ``snapshot_paths`` stores them before the referencing row.+ """with _ledger_lock():return _gc_blobs_locked()@@ -477,10 +481,14 @@ def _gc_blobs_locked() -> Tuple[int, int]:for item in (row.get("before") or []) + (row.get("after") or []):referenced.add(str(item.get("sha256", "")))deleted = freed = 0+ fresh_after = _time.time() - _BLOB_GC_GRACE_SECSfor blob in blobs.iterdir():if blob.is_file() and blob.name not in referenced:try:- size = blob.stat().st_size+ st = blob.stat()+ if st.st_mtime > fresh_after: # in-flight capture (incl. .tmp-*): row not appended yet+ continue+ size = st.st_sizeblob.unlink()except OSError:continuediff --git a/website/docs/user-guide/features/curator.md b/website/docs/user-guide/features/curator.mdindex c2e3f4b29c9..f298933f8ec 100644--- a/website/docs/user-guide/features/curator.md+++ b/website/docs/user-guide/features/curator.md@@ -175,7 +175,7 @@ skills:ledger: false```-The file is also size-bounded: once it grows past `skills.ledger_max_bytes` (default 5 MB), the next mutation first rewrites it through the unchanged-file dedup (exactly what `hermes curator ledger --compact` does) and, if genuinely-divergent entries still exceed the cap, drops the oldest ones regardless of shape — the newest entry always survives, lines in the retained tail are never rewritten or parsed (a malformed line there survives verbatim), and the sweep frees blobs nothing references anymore. Set it to `0` to keep the ledger append-only forever.+The file is also size-bounded: once it grows past `skills.ledger_max_bytes` (default 5 MB), the next mutation first rewrites it through the unchanged-file dedup (exactly what `hermes curator ledger --compact` does) and, if genuinely-divergent entries still exceed the cap, drops the oldest ones regardless of shape — the newest entry always survives, lines in the retained tail are never rewritten or parsed (a malformed line there survives verbatim), and the sweep frees blobs nothing references anymore and older than an hour (a fresh unreferenced blob may belong to a capture another process has not yet recorded). Set it to `0` to keep the ledger append-only forever.```yamlskills: