Skip to content

The agent that grows with you

fix(skill_ledger): hold .locks/ledger.lock across append + sweep and the CLI compact/GC path

kshitijk4poorc86607124f83
diff --git a/tests/tools/test_skill_ledger.py b/tests/tools/test_skill_ledger.py
index d549ecb8c75..21675c0a696 100644
--- a/tests/tools/test_skill_ledger.py
+++ b/tests/tools/test_skill_ledger.py
@@ -707,3 +707,73 @@ def test_trim_oldest_when_still_over_cap(ledger_env, monkeypatch):
fh.write(u_row)
assert skill_ledger._trim_oldest(len(u_row) + 8) >= 1
assert skill_ledger.ledger_path().read_bytes() == u_row, "a retained row containing U+2028 survives intact"
+
+
+def test_concurrent_appends_never_lose_a_middle_row(ledger_env, monkeypatch):
+ """Two writers appending while the maintenance sweep fires on (almost) every append:
+ every row each writer appended is either still in the ledger or was trimmed
+ oldest-first — never silently lost from the middle of a writer's sequence. The sweep's
+ read → ``os.replace`` must run under the same ``.locks/ledger.lock`` as the O_APPEND
+ write, or an append landing on the replaced inode vanishes (and ``gc_blobs`` would then
+ delete its blobs). The race is forced, not hoped for: writer A's first sweep pauses
+ between reading the ledger and replacing it until writer B has appended (or, when the
+ lock correctly blocks B, until a generous bound expires — green never depends on timing)."""
+ import threading
+
+ from tools import skill_ledger
+
+ import hermes_cli.config as _cfg
+
+ cap = {"skills": {"ledger_max_bytes": 4096}} # padded rows ~600 B: a trim on nearly every append
+ monkeypatch.setattr(_cfg, "load_config", lambda *a, **k: cap)
+ monkeypatch.setattr(_cfg, "load_config_readonly", lambda *a, **k: cap)
+
+ n, ids = 40, {"A": [], "B": []}
+ b_go, b_done = threading.Event(), threading.Event()
+ real_rewrite = skill_ledger._rewrite_ledger
+
+ def paused_rewrite(path, lines, op):
+ if threading.current_thread().name == "A" and not b_go.is_set():
+ b_go.set() # A has read the ledger; let B append now ...
+ b_done.wait(3.0) # ... and give it every chance to land before the replace
+ return real_rewrite(path, lines, op)
+
+ monkeypatch.setattr(skill_ledger, "_rewrite_ledger", paused_rewrite)
+ dropped, real_trim = [], skill_ledger._trim_oldest
+ monkeypatch.setattr(skill_ledger, "_trim_oldest",
+ lambda max_bytes: dropped.append(real_trim(max_bytes)) or dropped[-1])
+
+ def writer(k: str) -> None:
+ if k == "B":
+ b_go.wait(10.0)
+ for i in range(n):
+ before = [{"path": f"my-skill/{k}-{i}.md", "sha256": "a" * 64}]
+ after = [{"path": f"my-skill/{k}-{i}.md", "sha256": f"{i % 10}" * 64}]
+ ids[k].append(skill_ledger.append_entry(
+ "edit", "my-skill", before=before, after=after, evidence={"pad": "x" * 500}))
+ if k == "B":
+ b_done.set()
+
+ seeds = 8 # seed over the cap so A's very first append sweeps
+ 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})
+ threads = [threading.Thread(target=writer, args=(k,), name=k) for k in ids]
+ for t in threads:
+ t.start()
+ for t in threads:
+ t.join()
+
+ assert b_go.is_set(), "writer A's first append must have swept (test precondition)"
+ assert all(ids["A"]) and all(ids["B"]) and len(ids["A"]) == len(ids["B"]) == n, "every append reported success"
+ present = [json.loads(line)["id"] for line in
+ skill_ledger.ledger_path().read_text(encoding="utf-8").splitlines() if line.strip()]
+ assert present, "the newest row always survives a trim"
+ assert len(present) == seeds + 2 * n - sum(dropped), (
+ "every row is either in the ledger or was counted as trimmed — none silently lost"
+ )
+ for k, seq in ids.items():
+ survivors = [i for i in seq if i in set(present)]
+ assert survivors == seq[len(seq) - len(survivors):], (
+ f"writer {k}: rows missing from the middle — a concurrent sweep dropped an append"
+ )
diff --git a/tools/skill_ledger.py b/tools/skill_ledger.py
index 0df18d96455..da9165706a7 100644
--- a/tools/skill_ledger.py
+++ b/tools/skill_ledger.py
@@ -87,6 +87,16 @@ def blobs_dir() -> Path:
return get_hermes_home() / ".curator_backups" / "blobs"
+def _ledger_lock():
+ """Exclusive cross-process lock on the ledger file, held across an append AND the maintenance
+ sweep, and across the CLI compact/GC path. Without it a concurrent appender's O_APPEND write can
+ land on the inode ``compact_ledger``/``_trim_oldest`` are about to ``os.replace`` — the row is
+ silently lost and ``gc_blobs`` then deletes its blobs. Same idiom as ``_skill_mutation_lock``:
+ ``<skills>/.locks/ledger.lock``, thread-re-entrant, no-op where neither fcntl nor msvcrt exists."""
+ from tools.skill_usage import skill_file_lock
+ return skill_file_lock(_skills_dir() / ".locks" / "ledger.lock")
+
+
def _skills_cfg(key: str, default):
"""``skills.<key>`` from the read-only merged config (no deepcopy), or *default* when the
read fails. Lazy import keeps this module importable without the CLI."""
@@ -301,9 +311,10 @@ def append_entry(
"before": before or [], "after": after or []}
path = ledger_path()
path.parent.mkdir(parents=True, exist_ok=True)
- with open(path, "a", encoding="utf-8") as fh:
- fh.write(json.dumps(entry, ensure_ascii=False) + "\n")
- _maintain_size()
+ with _ledger_lock():
+ with open(path, "a", encoding="utf-8") as fh:
+ fh.write(json.dumps(entry, ensure_ascii=False) + "\n")
+ _maintain_size()
return entry["id"]
except Exception as e:
logger.warning("skill_ledger: failed to append entry (%s) — mutation unaffected", e)
@@ -370,6 +381,11 @@ def _trim_oldest(max_bytes: int) -> int:
the newest entry always survives. Lines in the retained tail are never rewritten
or parsed — malformed lines there survive verbatim; trimming drops the oldest
lines regardless of shape. Returns the number of lines dropped (0 = untouched)."""
+ with _ledger_lock():
+ return _trim_oldest_locked(max_bytes)
+
+
+def _trim_oldest_locked(max_bytes: int) -> int:
path = ledger_path()
raw = _read_ledger("trim skipped")
if raw is None:
@@ -400,6 +416,11 @@ def compact_ledger() -> Tuple[int, int, int]:
rollback semantics are preserved. Returns ``(entries, bytes_before, bytes_after)``. Atomic: the
new file replaces the old only once fully written. Malformed lines are kept verbatim. Follow with
``gc_blobs()``: dropped references leave blobs nothing can restore."""
+ with _ledger_lock():
+ return _compact_ledger_locked()
+
+
+def _compact_ledger_locked() -> Tuple[int, int, int]:
path = ledger_path()
raw = _read_ledger("compaction skipped")
if raw is None:
@@ -428,6 +449,11 @@ def gc_blobs() -> Tuple[int, int]:
write-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."""
+ with _ledger_lock():
+ return _gc_blobs_locked()
+
+
+def _gc_blobs_locked() -> Tuple[int, int]:
blobs = blobs_dir()
if not blobs.is_dir():
return 0, 0