The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(skills): share one _read_ledger() helper across compact/gc/list
kshitijk4poor5194561cc226
diff --git a/tools/skill_ledger.py b/tools/skill_ledger.pyindex 6cb1ee3a9ef..683cfcacf04 100644--- a/tools/skill_ledger.py+++ b/tools/skill_ledger.py@@ -296,18 +296,29 @@ def append_entry(return None+def _read_ledger(what: str, *, quiet_missing: bool = False) -> Optional[bytes]:+ """Raw ledger bytes, or ``None`` (after a warning) when the file cannot be read or is not UTF-8.+ ``quiet_missing`` keeps a merely absent ledger silent — normal for a fresh install."""+ try:+ raw = ledger_path().read_bytes()+ raw.decode("utf-8")+ return raw+ except (OSError, UnicodeError) as exc:+ if not (quiet_missing and isinstance(exc, FileNotFoundError)):+ logger.warning("skill_ledger: ledger unreadable (%s); %s", exc, what)+ return None++def compact_ledger() -> Tuple[int, int, int]:"""Rewrite the ledger with every entry's unchanged paths dropped (see ``_delta``); ids, order androllback semantics are preserved. Returns ``(entries, bytes_before, bytes_after)``. Atomic: thenew file replaces the old only once fully written. Malformed lines are kept verbatim. Follow with``gc_blobs()``: dropped references leave blobs nothing can restore."""path = ledger_path()- try:- raw = path.read_bytes()- text = raw.decode("utf-8")- except (OSError, UnicodeError) as exc:- logger.warning("skill_ledger: ledger unreadable (%s); compaction skipped", exc)+ raw = _read_ledger("compaction skipped")+ if raw is None:return 0, 0, 0+ text = raw.decode("utf-8")out, kept = [], 0for line in text.splitlines():if not line.strip():@@ -338,13 +349,10 @@ def gc_blobs() -> Tuple[int, int]:if not blobs.is_dir():return 0, 0referenced: set = set()- try:- lines = ledger_path().read_text(encoding="utf-8").splitlines()- except (OSError, UnicodeError) as exc:- # An unavailable ledger is not evidence that its blobs are unreferenced.- logger.warning("skill_ledger: ledger unreadable (%s); blob GC skipped", exc)+ raw = _read_ledger("blob GC skipped")+ if raw is None: # an unavailable ledger is not evidence that its blobs are unreferencedreturn 0, 0- for line in lines:+ for line in raw.decode("utf-8").splitlines():if not line.strip():continuetry:@@ -409,14 +417,11 @@ def capture_before(def list_entries(skill: Optional[str] = None, limit: Optional[int] = None) -> List[Dict[str, Any]]:"""Read the ledger, newest first. Malformed lines are skipped."""- try:- lines = ledger_path().read_text(encoding="utf-8").splitlines()- except (OSError, UnicodeError) as exc: # missing, unreadable or undecodable ledger == empty- if not isinstance(exc, FileNotFoundError): # a missing ledger is normal; a corrupt one is not- logger.warning("skill_ledger: ledger unreadable (%s); listing empty", exc)+ raw = _read_ledger("listing empty", quiet_missing=True) # missing/unreadable/undecodable == empty+ if raw is None:return []rows: List[Dict[str, Any]] = []- for line in lines:+ for line in raw.decode("utf-8").splitlines():with suppress(json.JSONDecodeError):row = json.loads(line) if line.strip() else Noneif isinstance(row, dict) and (not skill or row.get("skill") == skill):