The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(curator): ledger trim stops at a low-water mark
kshitijk4poor74069eb42854
diff --git a/tests/tools/test_skill_ledger.py b/tests/tools/test_skill_ledger.pyindex 89371ab6dd3..d908de3fab1 100644--- a/tests/tools/test_skill_ledger.py+++ b/tests/tools/test_skill_ledger.py@@ -638,8 +638,9 @@ def test_auto_compact_triggers_at_threshold(ledger_env, monkeypatch):def test_trim_oldest_when_still_over_cap(ledger_env, monkeypatch):"""When compaction alone cannot reach the cap (every entry genuinely- differs), the oldest entries are dropped until it fits — newest entries- survive, malformed lines are kept verbatim."""+ differs), the oldest entries are dropped until it fits under the LOW-WATER+ mark (80% of the cap, so the next append does not immediately re-trigger the+ sweep) — newest entries survive, malformed lines are kept verbatim."""from tools import skill_ledgerimport hermes_cli.config as _cfg@@ -666,6 +667,13 @@ def test_trim_oldest_when_still_over_cap(ledger_env, monkeypatch):raw = skill_ledger.ledger_path().read_text(encoding="utf-8")assert "{not json at all" in rawassert skill_ledger.ledger_path().stat().st_size <= 8192+ # A sweep that fires does not stop at the cap but at the low-water mark, so the+ # next few appends ride under the cap without paying compact+trim+gc again.+ skill_ledger.append_entry(+ "edit", "my-skill", before=[{"path": "my-skill/z.md", "sha256": "a" * 64}],+ after=[{"path": "my-skill/z2.md", "sha256": "b" * 64}], evidence={"pad": "z" * 3000})+ assert skill_ledger.ledger_path().stat().st_size <= int(8192 * 0.8)+ assert "{not json at all" in skill_ledger.ledger_path().read_text(encoding="utf-8")def test_disabled_threshold_never_touches_the_file(ledger_env, monkeypatch):diff --git a/tools/skill_ledger.py b/tools/skill_ledger.pyindex 0a5f2aa98eb..d2bb967109b 100644--- a/tools/skill_ledger.py+++ b/tools/skill_ledger.py@@ -329,7 +329,13 @@ def _maintain_size() -> None:"""Keep the ledger bounded: once it crosses ``skills.ledger_max_bytes`` run thedelta-dedup rewrite, and if genuinely-divergent entries still exceed the cap,drop the oldest ones until it fits. Best-effort telemetry, never a gate: any- failure is logged and the just-appended entry stays on disk."""+ failure is logged and the just-appended entry stays on disk.++ Trimming stops at a LOW-WATER mark (``_TRIM_LOW_WATER`` of the cap), not at the+ cap itself: a ledger trimmed to exactly the cap is over it again on the very next+ append, so every append would pay compact + trim + blob GC. ``gc_blobs`` only+ runs when the trim actually dropped rows — that is the only way a blob can+ become orphaned here."""max_bytes = _max_ledger_bytes()if max_bytes <= 0:return@@ -337,22 +343,27 @@ def _maintain_size() -> None:if ledger_path().stat().st_size <= max_bytes:return_, _, size_after = compact_ledger()- if size_after > max_bytes:- _trim_oldest(max_bytes)- gc_blobs()+ low_water = int(max_bytes * _TRIM_LOW_WATER)+ dropped = _trim_oldest(low_water) if size_after > low_water else 0+ if dropped:+ gc_blobs()except Exception as e:logger.warning("skill_ledger: maintenance sweep failed (%s) — ledger left as-is", e)-def _trim_oldest(max_bytes: int) -> None:+_TRIM_LOW_WATER = 0.8 # trim target as a fraction of ``skills.ledger_max_bytes``+++def _trim_oldest(max_bytes: int) -> int:"""Rewrite the ledger without its oldest parsed entries until it is at most- *max_bytes*; the newest entry always survives, malformed lines are kept verbatim."""+ *max_bytes*; the newest entry always survives, malformed lines are kept verbatim.+ Returns the number of lines dropped (0 = file untouched)."""path = ledger_path()raw = _read_ledger("trim skipped")if raw is None:- return+ return 0lines = raw.decode("utf-8").splitlines()newest_json = Nonefor line in reversed(lines):@@ -370,10 +381,15 @@ def _trim_oldest(max_bytes: int) -> None:kept.reverse()data = ("\n".join(kept) + "\n").encode("utf-8") if kept else b""if newest_json is not None and newest_json not in kept:+ kept = [newest_json]data = (newest_json + "\n").encode("utf-8")+ dropped = len(lines) - len(kept)+ if dropped <= 0:+ return 0tmp = path.with_name(path.name + ".trim.tmp")tmp.write_bytes(data)os.replace(tmp, path)+ return droppeddef compact_ledger() -> Tuple[int, int, int]: