The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(agent/curator): table-driven REPORT.md sections, phase helpers for the LLM fork, shared mkdir/extract helpers in curator_backup
Teknium3b17aa46cc49
diff --git a/agent/curator.py b/agent/curator.pyindex 285c5701006..15edd4cd765 100644--- a/agent/curator.py+++ b/agent/curator.py@@ -52,18 +52,18 @@ def _default_state() -> Dict[str, Any]:def load_state() -> Dict[str, Any]:+ base = _default_state()path = _state_file()if not path.exists():- return _default_state()+ return basetry:data = json.loads(path.read_text(encoding="utf-8"))- if isinstance(data, dict):- base = _default_state()- base.update({k: v for k, v in data.items() if k in base or k.startswith("_")})- return baseexcept (OSError, json.JSONDecodeError) as e:logger.debug("Failed to read curator state: %s", e)- return _default_state()+ return base+ if isinstance(data, dict):+ base.update({k: v for k, v in data.items() if k in base or k.startswith("_")})+ return basedef save_state(data: Dict[str, Any]) -> None:@@ -104,7 +104,6 @@ def _read_config_section(*path: str, label: str, log: logging.Logger = logger) -def _load_config() -> Dict[str, Any]:- """Read curator.* config."""return _read_config_section("curator", label="curator")@@ -137,26 +136,22 @@ def get_archive_after_days() -> int:def get_prune_builtins() -> bool:- """Bundled built-ins are curation candidates (ON by default); they age out like- agent-created skills and a suppression list keeps them archived across- `hermes update` re-seeds. Hub-installed skills are never pruned."""+ """Bundled built-ins are curation candidates (ON by default); a suppression+ list keeps them archived across `hermes update` re-seeds. Hub skills never."""return bool(_load_config().get("prune_builtins", True))def get_consolidate() -> bool:- """Whether a run includes the LLM consolidation pass. OFF by default: only the- deterministic prune runs, no aux-model fork. ``hermes curator run- --consolidate`` overrides per invocation."""+ """LLM consolidation pass — OFF by default (prune only, no aux-model fork);+ ``hermes curator run --consolidate`` overrides per invocation."""return bool(_load_config().get("consolidate", DEFAULT_CONSOLIDATE))# --- Idle / interval check ---def _parse_iso(ts: Optional[str]) -> Optional[datetime]:- if not ts:- return Nonetry:- return datetime.fromisoformat(ts)+ return datetime.fromisoformat(ts) if ts else Noneexcept (TypeError, ValueError):return None@@ -193,11 +188,8 @@ def should_run_now(now: Optional[datetime] = None) -> bool:# --- Automatic state transitions (pure function, no LLM) ---def _cron_referenced_skills() -> Set[str]:- """Skill names referenced by any cron job (incl. paused/disabled).-- Best-effort: a cron import error or corrupt jobs store must never break the- curator, so any failure yields an empty set (no protection, but no crash).- """+ """Skill names referenced by any cron job (incl. paused/disabled). Best-effort:+ a cron import error or corrupt jobs store yields an empty set, never a crash."""try:from cron.jobs import referenced_skill_names as _refsreturn _refs()@@ -236,22 +228,21 @@ def apply_automatic_transitions(now: Optional[datetime] = None) -> Dict[str, intnow = datetime.now(timezone.utc)stale_cutoff = now - timedelta(days=get_stale_after_days())archive_cutoff = now - timedelta(days=get_archive_after_days())-- cron_referenced = _cron_referenced_skills()+ # Cron-referenced skills are in use by definition (usage only bumps when a+ # job fires, so paused/rare jobs would age them out). Treat as pinned.+ protected = _cron_referenced_skills()counts = {"marked_stale": 0, "archived": 0, "reactivated": 0, "checked": 0, "seeded": 0}+ def _set(name: str, state: str, key: str) -> None:+ _u.set_state(name, state)+ counts[key] += 1+for row in _u.curated_report():counts["checked"] += 1name = row["name"]- if row.get("pinned"):- continue-- # Cron-referenced skills are in use by definition (usage only bumps when- # a job fires, so paused/rare jobs would age them out). Treat as pinned.- if name in cron_referenced:+ if row.get("pinned") or name in protected:continue-# First sight with no persisted record: anchor its clock to now and defer.if not row.get("_persisted", True):_u.seed_record_if_missing(name)@@ -259,32 +250,25 @@ def apply_automatic_transitions(now: Optional[datetime] = None) -> Dict[str, intcontinue# Never-active skills anchor on created_at so they don't self-archive.- last_activity = _parse_iso(row.get("last_activity_at"))- anchor = last_activity or _parse_iso(row.get("created_at")) or now+ anchor = _parse_iso(row.get("last_activity_at")) or _parse_iso(row.get("created_at")) or nowif anchor.tzinfo is None:anchor = anchor.replace(tzinfo=timezone.utc)-current = row.get("state", _u.STATE_ACTIVE)# use_count == 0 is absence of evidence, not staleness: never archive a# never-used skill younger than stale_after_days.- never_used = int(row.get("use_count", 0) or 0) == 0- if never_used and anchor > stale_cutoff:+ if int(row.get("use_count", 0) or 0) == 0 and anchor > stale_cutoff:if current == _u.STATE_STALE:- _u.set_state(name, _u.STATE_ACTIVE)- counts["reactivated"] += 1+ _set(name, _u.STATE_ACTIVE, "reactivated")continueif anchor <= archive_cutoff and current != _u.STATE_ARCHIVED:if _archive_as_curator(_u, name):counts["archived"] += 1elif anchor <= stale_cutoff and current == _u.STATE_ACTIVE:- _u.set_state(name, _u.STATE_STALE)- counts["marked_stale"] += 1+ _set(name, _u.STATE_STALE, "marked_stale")elif anchor > stale_cutoff and current == _u.STATE_STALE:- # Used again after being marked stale — reactivate.- _u.set_state(name, _u.STATE_ACTIVE)- counts["reactivated"] += 1+ _set(name, _u.STATE_ACTIVE, "reactivated") # used again after going stalereturn counts@@ -496,6 +480,7 @@ CURATOR_PRUNE_BUILTINS_NOTE = ()+# --- Per-run reports — {YYYYMMDD-HHMMSS}/run.json + REPORT.md under logs/curator/ ---def _reports_root() -> Path:@@ -549,9 +534,7 @@ def _find_reference(args: Dict[str, Any], needles: Set[str]) -> Optional[str]:hay = args.get(key)if not isinstance(hay, str):continue- for needle in needles:- if not needle:- continue+ for needle in filter(None, needles):if (_needle_in_path_component(needle, hay) if key == "file_path"else re.search(rf'\b{re.escape(needle)}\b', hay)):return hay@@ -570,16 +553,10 @@ def _classify_removed_skills(Returns ``{"consolidated": [{name, into, evidence}], "pruned": [{name}]}``."""consolidated: List[Dict[str, Any]] = []pruned: List[Dict[str, Any]] = []-- parsed_calls = [- args for args in (_skill_manage_args(tc, raw_fallback=True) for tc in tool_calls or [])- if args is not None- ]+ parsed_calls = [a for a in (_skill_manage_args(tc, raw_fallback=True) for tc in tool_calls or []) if a is not None]destinations = set(after_names) | set(added or [])- for name in removed:- if not name:- continue+ for name in filter(None, removed):needles = {name, name.replace("-", "_"), name.replace("_", "-")}for args in parsed_calls:target = args.get("name")@@ -603,20 +580,16 @@ def _clean_str(value: Any) -> str:return value.strip() if isinstance(value, str) else ""-def _parse_structured_summary(- llm_final: str,-) -> Dict[str, List[Dict[str, str]]]:+def _parse_structured_summary(llm_final: str) -> Dict[str, List[Dict[str, str]]]:"""Extract the required fenced ```yaml block (``consolidations:`` /``prunings:`` lists) from the curator's final response. Tolerant: missingblock or malformed YAML → empty lists (caller falls back to the tool-callheuristic); a partial block returns what parsed.Returns ``{"consolidations": [{from, into, reason}], "prunings": [{name, reason}]}``."""out: Dict[str, List[Dict[str, str]]] = {"consolidations": [], "prunings": []}- if not llm_final or not isinstance(llm_final, str):- return out# Match ```yaml specifically so a code sample the model quoted elsewhere is# never mistaken for the summary.- match = re.search(r"```ya?ml\s*\n(.*?)\n```", llm_final, re.DOTALL | re.IGNORECASE)+ match = re.search(r"```ya?ml\s*\n(.*?)\n```", llm_final, re.DOTALL | re.IGNORECASE) if isinstance(llm_final, str) else Noneif not match:return outtry:@@ -642,9 +615,7 @@ def _parse_structured_summary(return out-def _extract_absorbed_into_declarations(- tool_calls: List[Dict[str, Any]],-) -> Dict[str, Dict[str, Any]]:+def _extract_absorbed_into_declarations(tool_calls: List[Dict[str, Any]]) -> Dict[str, Dict[str, Any]]:"""Model-declared absorption targets from ``skill_manage(action='delete')``calls — the authoritative classification signal (beats YAML parsing andsubstring heuristics). Returns ``{name: {"into": umbrella | "", "declared": True}}``;@@ -655,9 +626,7 @@ def _extract_absorbed_into_declarations(args = _skill_manage_args(tc, raw_fallback=False)if args is None or args.get("action") != "delete":continue- name = args.get("name")- # absorbed_into must be present (empty string is meaningful).- target = args.get("absorbed_into")+ name, target = args.get("name"), args.get("absorbed_into")if isinstance(name, str) and name.strip() and isinstance(target, str):out[name.strip()] = {"into": target.strip(), "declared": True}return out@@ -683,47 +652,35 @@ def _reconcile_classification(model_cons = {e["from"]: e for e in model_block.get("consolidations", [])}model_pruned = {e["name"]: e for e in model_block.get("prunings", [])}declared = absorbed_declarations or {}-consolidated: List[Dict[str, Any]] = []pruned: List[Dict[str, Any]] = []for name in removed:- mc = model_cons.get(name)- mp = model_pruned.get(name)- hc = heur_cons.get(name)- dec = declared.get(name)-- def _cons(into: str, source: str, reason: str = "", *, with_hc: bool = False, **extra: Any) -> None:- entry: Dict[str, Any] = {"name": name, "into": into, "source": source, "reason": reason}- if with_hc and hc and hc.get("evidence"):- entry["evidence"] = hc["evidence"]- entry.update(extra)- consolidated.append(entry)+ mc, mp, hc, dec = model_cons.get(name), model_pruned.get(name), heur_cons.get(name), declared.get(name)+ mc_reason = (mc.get("reason") or "") if mc else ""+ mp_reason = (mp.get("reason") or "") if mp else ""+ hc_evidence = {"evidence": hc["evidence"]} if hc and hc.get("evidence") else {}++ def _cons(into: str, source: str, reason: str = "", **extra: Any) -> None:+ consolidated.append({"name": name, "into": into, "source": source, "reason": reason, **extra})def _prune(source: str, reason: str = "") -> None:pruned.append({"name": name, "source": source, "reason": reason})- if dec is not None:- into_claim = dec.get("into", "")- if into_claim and into_claim in destinations:- _cons(into_claim, "absorbed_into (model-declared at delete)",- (mc.get("reason") or "") if mc else "", with_hc=True)- continue- if into_claim == "":- _prune("absorbed_into=\"\" (model-declared prune)", (mp.get("reason") or "") if mp else "")- continue-- if mc and mc.get("into") in destinations:- _cons(mc["into"], "model" + ("+audit" if hc else ""), mc.get("reason") or "", with_hc=True)- elif mc: # model named a missing umbrella- if hc:- _cons(hc["into"], "tool-call audit (model named missing umbrella)",- evidence=hc.get("evidence", ""), model_claimed_into=mc["into"])- else:- _prune("fallback (model named missing umbrella, no tool-call evidence)")+ into_claim = dec.get("into", "") if dec is not None else None+ if into_claim and into_claim in destinations:+ _cons(into_claim, "absorbed_into (model-declared at delete)", mc_reason, **hc_evidence)+ elif into_claim == "":+ _prune("absorbed_into=\"\" (model-declared prune)", mp_reason)+ elif mc and mc.get("into") in destinations:+ _cons(mc["into"], "model" + ("+audit" if hc else ""), mc_reason, **hc_evidence)+ elif mc and hc: # model named a missing umbrella; the audit found the real one+ _cons(hc["into"], "tool-call audit (model named missing umbrella)",+ evidence=hc.get("evidence", ""), model_claimed_into=mc["into"])+ elif mc:+ _prune("fallback (model named missing umbrella, no tool-call evidence)")elif hc:- _cons(hc["into"], "tool-call audit (model omitted from structured block)",- evidence=hc.get("evidence", ""))+ _cons(hc["into"], "tool-call audit (model omitted from structured block)", evidence=hc.get("evidence", ""))else:_prune("model" if mp else "no-evidence fallback", mp.get("reason", "") if mp else "")@@ -739,22 +696,16 @@ class _RunDiff(NamedTuple):def _diff_and_classify(- before_names: Set[str],- after_names: Set[str],- tool_calls: List[Dict[str, Any]],- model_final: str,+ before_names: Set[str], after_names: Set[str], tool_calls: List[Dict[str, Any]], model_final: str,) -> _RunDiff:"""Diff the before/after skill sets and classify every removal: the model'sYAML block carries intent + rationale, the tool-call heuristic audits forhallucinated umbrellas/omissions, per-delete ``absorbed_into`` beats both."""removed = sorted(before_names - after_names)added = sorted(after_names - before_names)- heuristic = _classify_removed_skills(- removed=removed, added=added, after_names=after_names, tool_calls=tool_calls,- )classification = _reconcile_classification(removed=removed,- heuristic=heuristic,+ heuristic=_classify_removed_skills(removed=removed, added=added, after_names=after_names, tool_calls=tool_calls),model_block=_parse_structured_summary(model_final),destinations=set(after_names) | set(added),absorbed_declarations=_extract_absorbed_into_declarations(tool_calls),@@ -762,18 +713,18 @@ def _diff_and_classify(return _RunDiff(after_names, removed, added, classification["consolidated"], classification["pruned"])+def _by_name(report: List[Dict[str, Any]]) -> Dict[Any, Dict[str, Any]]:+ return {r.get("name"): r for r in report if isinstance(r, dict)}++def _build_rename_summary(- *,- before_names: Set[str],- after_report: List[Dict[str, Any]],- tool_calls: List[Dict[str, Any]],- model_final: str,+ *, before_names: Set[str], after_report: List[Dict[str, Any]], tool_calls: List[Dict[str, Any]], model_final: str,) -> str:"""The "where did my skills go?" lines appended to the user-visible``final_summary``; "" when nothing was archived. Capped at 10 entries so abig consolidation doesn't flood agent.log (full list is in REPORT.md); thepin hint appears only when a consolidation produced an umbrella."""- after_names = {r.get("name") for r in after_report if isinstance(r, dict)}+ after_names = set(_by_name(after_report))if not before_names - after_names:return ""diff = _diff_and_classify(before_names, after_names, tool_calls, model_final)@@ -781,10 +732,7 @@ def _build_rename_summary(SHOW = 10total = len(diff.consolidated) + len(diff.pruned)entries = [f" • {e.get('name', '?')} → {e.get('into', '?')}" for e in diff.consolidated]- entries += [- f" • {e.get('name', '?') if isinstance(e, dict) else e} — pruned (stale)"- for e in diff.pruned- ]+ entries += [f" • {e.get('name', '?') if isinstance(e, dict) else e} — pruned (stale)" for e in diff.pruned]lines = [f"archived {total} skill(s):"] + entries[:SHOW]if total > SHOW:lines.append(f" … and {total - SHOW} more")@@ -826,6 +774,23 @@ def _write_json(path: Path, payload: Any, label: str) -> None:_write_file(path, label, lambda: json.dumps(payload, indent=2, ensure_ascii=False) + "\n")+def _new_run_dir(started_at: datetime) -> Optional[Path]:+ """``logs/curator/{YYYYMMDD-HHMMSS}[-N]/`` (N disambiguates a crash-rerun in+ the same second); None when it can't be created."""+ root = _reports_root()+ stamp = started_at.strftime("%Y%m%d-%H%M%S")+ run_dir, suffix = root / stamp, 1+ while run_dir.exists():+ suffix += 1+ run_dir = root / f"{stamp}-{suffix}"+ try:+ run_dir.mkdir(parents=True, exist_ok=False)+ except Exception as e:+ logger.debug("Curator run dir create failed: %s", e)+ return None+ return run_dir++def _write_run_report(*,started_at: datetime,@@ -839,29 +804,13 @@ def _write_run_report() -> Optional[Path]:"""Write run.json + REPORT.md under logs/curator/{YYYYMMDD-HHMMSS}/. Returnsthe report dir, or None if it couldn't be created (reporting is best-effort)."""- root = _reports_root()- try:- root.mkdir(parents=True, exist_ok=True)- except Exception as e:- logger.debug("Curator report dir create failed: %s", e)- return None- stamp = started_at.strftime("%Y%m%d-%H%M%S")- run_dir, suffix = root / stamp, 1 # crash-rerun within the same second gets a disambiguator- while run_dir.exists():- suffix += 1- run_dir = root / f"{stamp}-{suffix}"- try:- run_dir.mkdir(parents=True, exist_ok=False)- except Exception as e:- logger.debug("Curator run dir create failed: %s", e)+ run_dir = _new_run_dir(started_at)+ if run_dir is None:return Nonetool_calls = llm_meta.get("tool_calls", []) or []- after_by_name = {r.get("name"): r for r in after_report if isinstance(r, dict)}- before_by_name = {r.get("name"): r for r in before_report if isinstance(r, dict)}- diff = _diff_and_classify(- before_names, set(after_by_name), tool_calls, llm_meta.get("final", "") or ""- )+ after_by_name, before_by_name = _by_name(after_report), _by_name(before_report)+ diff = _diff_and_classify(before_names, set(after_by_name), tool_calls, llm_meta.get("final", "") or "")transitions: List[Dict[str, str]] = []for name in sorted(diff.after_names & before_names):@@ -871,8 +820,8 @@ def _write_run_report(transitions.append({"name": name, "from": s_before, "to": s_after})tc_counts: Dict[str, int] = dict(Counter(tc.get("name", "unknown") for tc in tool_calls))-cron_rewrites = _rewrite_cron_refs(diff.consolidated, diff.pruned)+ jobs_updated = int(cron_rewrites.get("jobs_updated", 0))payload = {"started_at": started_at.isoformat(),@@ -889,7 +838,7 @@ def _write_run_report("consolidated_this_run": len(diff.consolidated),"pruned_this_run": len(diff.pruned),"state_transitions": len(transitions),- "cron_jobs_rewritten": int(cron_rewrites.get("jobs_updated", 0)),+ "cron_jobs_rewritten": jobs_updated,"tool_calls_total": sum(tc_counts.values()),},"tool_call_counts": tc_counts,@@ -908,24 +857,83 @@ def _write_run_report(_write_json(run_dir / "run.json", payload, "run.json")_write_file(run_dir / "REPORT.md", "REPORT.md", lambda: _render_report_markdown(payload))- # Only when a job was touched, to keep no-op run dirs uncluttered.- if int(cron_rewrites.get("jobs_updated", 0)) > 0:+ if jobs_updated > 0: # only when a job was touched, to keep no-op run dirs uncluttered_write_json(run_dir / "cron_rewrites.json", cron_rewrites, "cron_rewrites.json")return run_dir+def _reason_suffix(entry: Dict[str, Any]) -> str:+ reason = (entry.get("reason") or "").strip()+ return f" — {reason}" if reason else ""+++def _consolidated_lines(entry: Dict[str, Any]) -> List[str]:+ line = f"- `{entry.get('name', '?')}` → merged into `{entry.get('into', '?')}`" + _reason_suffix(entry)+ source = entry.get("source", "")+ if source and source.startswith("tool-call audit"):+ line += f" _(detected via {source})_" # model didn't enumerate this one — explains the missing rationale+ lines = [line]+ if entry.get("model_claimed_into"):+ lines.append(+ f" ⚠ The curator's summary named `{entry['model_claimed_into']}` "+ "as the umbrella but that skill doesn't exist post-run; "+ "showing the tool-call audit's finding instead."+ )+ return lines+++def _pruned_lines(entry: Any) -> List[str]:+ # Reconciler entries are dicts {name, source, reason}; tolerate bare strings (older format).+ return [f"- `{entry.get('name', '?')}`" + _reason_suffix(entry) if isinstance(entry, dict) else f"- `{entry}`"]+++def _cron_rewrite_lines(entry: Dict[str, Any]) -> List[str]:+ job_name = entry.get("job_name") or entry.get("job_id") or "?"+ before, after = entry.get("before") or [], entry.get("after") or []+ return (+ [f"- `{job_name}`: `{', '.join(before)}` → `{', '.join(after) or '(none)'}`"]+ + [f" - `{old}` → `{new}` (consolidated)" for old, new in (entry.get("mapped") or {}).items()]+ + [f" - `{name}` dropped (pruned)" for name in (entry.get("dropped") or [])]+ )+++# REPORT.md list sections, in order: (payload key, heading, intro, per-entry renderer, cap, overflow hint).+# Consolidated entries are archived (recoverable by design) but live on inside the umbrella.+_REPORT_SECTIONS = (+ ("consolidated", "Consolidated into umbrella skills",+ "_These skills were **absorbed into another skill** during this run — "+ "their content still lives, just under a different name. "+ "The original directory was moved to `~/.hermes/skills/.archive/` for "+ "safety and can be restored via `hermes curator restore <name>` if the "+ "consolidation was wrong._\n", _consolidated_lines, 50, "see `run.json`"),+ ("pruned", "Pruned — archived for staleness",+ "_These skills were archived without being merged into an umbrella "+ "(e.g. stale, unused, or judged irrelevant). "+ "Directories live under `~/.hermes/skills/.archive/`. "+ "Restore any via `hermes curator restore <name>`._\n", _pruned_lines, 50, "see `run.json`"),+ ("added", "New skills this run",+ "_Usually these are new class-level umbrellas created via `skill_manage action=create`._\n",+ lambda n: [f"- `{n}`"], None, ""),+ ("state_transitions", "State transitions", None,+ lambda t: [f"- `{t.get('name')}`: {t.get('from')} → {t.get('to')}"], None, ""),+ ("cron_rewrites", "Cron job skill references rewritten",+ "_Cron jobs that referenced a consolidated or pruned skill were "+ "updated in-place so they keep loading the right instructions "+ "on their next run. See `cron_rewrites.json` for the full record._\n",+ _cron_rewrite_lines, 25, "see `cron_rewrites.json`"),+)++def _render_report_markdown(p: Dict[str, Any]) -> str:"""Render the human-readable REPORT.md."""- lines: List[str] = []- duration = p.get("duration_seconds", 0) or 0- mins, secs = divmod(int(duration), 60)+ mins, secs = divmod(int(p.get("duration_seconds", 0) or 0), 60)dur_label = f"{mins}m {secs}s" if mins else f"{secs}s"counts = p.get("counts") or {}auto = p.get("auto_transitions") or {}tc_counts = p.get("tool_call_counts") or {}error = p.get("llm_error")- lines += [+ lines = [f"# Curator run — {p.get('started_at', '')}\n",f"Model: `{p.get('model') or '(not resolved)'}` via `{p.get('provider') or '(not resolved)'}` · "f"Duration: {dur_label} · "@@ -934,7 +942,6 @@ def _render_report_markdown(p: Dict[str, Any]) -> str:]if error:lines.append(f"> ⚠ LLM pass error: `{error}`\n")-lines += ["## Auto-transitions (pure, no LLM)\n",f"- checked: {auto.get('checked', 0)}",@@ -953,91 +960,21 @@ def _render_report_markdown(p: Dict[str, Any]) -> str:"",]- def _overflow(items: list, show: int, hint: str) -> None:- if len(items) > show:+ for key, title, intro, render, show, hint in _REPORT_SECTIONS:+ items = p.get(key) or []+ if key == "cron_rewrites": # lets users audit that the auto-rewrite did the right thing+ items = items.get("rewrites") or []+ if not items:+ continue+ lines.append(f"### {title} ({len(items)})\n")+ if intro:+ lines.append(intro)+ for entry in items[:show]:+ lines += render(entry)+ if show is not None and len(items) > show:lines.append(f"- … and {len(items) - show} more ({hint})")lines.append("")- def _reason(entry: Dict[str, Any]) -> str:- reason = (entry.get("reason") or "").strip()- return f" — {reason}" if reason else ""-- # Consolidated — the directory is archived (recoverable by design) but the- # live content continues inside the destination umbrella.- consolidated = p.get("consolidated") or []- if consolidated:- lines += [- f"### Consolidated into umbrella skills ({len(consolidated)})\n",- "_These skills were **absorbed into another skill** during this run — "- "their content still lives, just under a different name. "- "The original directory was moved to `~/.hermes/skills/.archive/` for "- "safety and can be restored via `hermes curator restore <name>` if the "- "consolidation was wrong._\n",- ]- for entry in consolidated[:50]:- line = f"- `{entry.get('name', '?')}` → merged into `{entry.get('into', '?')}`" + _reason(entry)- source = entry.get("source", "")- if source and source.startswith("tool-call audit"):- # The model didn't enumerate this one — explains the missing rationale.- line += f" _(detected via {source})_"- lines.append(line)- if entry.get("model_claimed_into"):- lines.append(- f" ⚠ The curator's summary named `{entry['model_claimed_into']}` "- "as the umbrella but that skill doesn't exist post-run; "- "showing the tool-call audit's finding instead."- )- _overflow(consolidated, 50, "see `run.json`")-- pruned = p.get("pruned") or []- if pruned:- lines += [- f"### Pruned — archived for staleness ({len(pruned)})\n",- "_These skills were archived without being merged into an umbrella "- "(e.g. stale, unused, or judged irrelevant). "- "Directories live under `~/.hermes/skills/.archive/`. "- "Restore any via `hermes curator restore <name>`._\n",- ]- for entry in pruned[:50]:- # Reconciler entries are dicts {name, source, reason}; tolerate bare strings (older format).- if isinstance(entry, dict):- lines.append(f"- `{entry.get('name', '?')}`" + _reason(entry))- else:- lines.append(f"- `{entry}`")- _overflow(pruned, 50, "see `run.json`")-- added = p.get("added") or []- if added:- lines += [- f"### New skills this run ({len(added)})\n",- "_Usually these are new class-level umbrellas created via `skill_manage action=create`._\n",- ]- lines += [f"- `{n}`" for n in added]- lines.append("")-- trans = p.get("state_transitions") or []- if trans:- lines.append(f"### State transitions ({len(trans)})\n")- lines += [f"- `{t.get('name')}`: {t.get('from')} → {t.get('to')}" for t in trans]- lines.append("")-- # Cron rewrites — lets users audit that the auto-rewrite did the right thing.- cron_rewrites_list = (p.get("cron_rewrites") or {}).get("rewrites") or []- if cron_rewrites_list:- lines += [- f"### Cron job skill references rewritten ({len(cron_rewrites_list)})\n",- "_Cron jobs that referenced a consolidated or pruned skill were "- "updated in-place so they keep loading the right instructions "- "on their next run. See `cron_rewrites.json` for the full record._\n",- ]- for entry in cron_rewrites_list[:25]:- job_name = entry.get("job_name") or entry.get("job_id") or "?"- before, after = entry.get("before") or [], entry.get("after") or []- lines.append(f"- `{job_name}`: `{', '.join(before)}` → `{', '.join(after) or '(none)'}`")- lines += [f" - `{old}` → `{new}` (consolidated)" for old, new in (entry.get("mapped") or {}).items()]- lines += [f" - `{name}` dropped (pruned)" for name in (entry.get("dropped") or [])]- _overflow(cron_rewrites_list, 25, "see `cron_rewrites.json`")-final = (p.get("llm_final") or "").strip()if final:lines += ["## LLM final summary\n", final, ""]@@ -1092,6 +1029,43 @@ def _safe_curated_report() -> List[Dict[str, Any]]:return []+def _consolidation_pass(prefix: str, auto_summary: str, dry_run: bool, before_names: Set[str]) -> tuple:+ """The LLM half of a run: fork (unless no candidates), then append the rename+ map (`old-name → umbrella`) so users needn't dig into REPORT.md.+ Returns ``(final_summary, llm_meta)``; never raises."""+ try:+ candidate_list = _render_candidate_list()+ if "No agent-created skills" in candidate_list:+ final_summary = f"{prefix}{auto_summary}; llm: skipped (no candidates)"+ llm_meta = _llm_meta("skipped (no candidates)")+ else:+ # With prune-builtins on, bundled skills are candidates too:+ # relax hard rule #1 for them (archive only; hub stays off-limits).+ builtins_note = CURATOR_PRUNE_BUILTINS_NOTE if get_prune_builtins() else ""+ prompt = f"{CURATOR_REVIEW_PROMPT}{builtins_note}\n\n{candidate_list}"+ if dry_run:+ prompt = f"{CURATOR_DRY_RUN_BANNER}\n\n{prompt}"+ llm_meta = _run_llm_review(prompt)+ final_summary = f"{prefix}{auto_summary}; llm: {llm_meta.get('summary', 'no change')}"+ except Exception as e:+ logger.debug("Curator LLM pass failed: %s", e, exc_info=True)+ final_summary = f"{prefix}{auto_summary}; llm: error ({e})"+ llm_meta = _llm_meta(f"error ({e})", str(e))++ try: # best-effort: never block the run on formatting+ rename_lines = _build_rename_summary(+ before_names=before_names,+ after_report=skill_usage.curated_report(),+ tool_calls=llm_meta.get("tool_calls", []) or [],+ model_final=llm_meta.get("final", "") or "",+ )+ if rename_lines:+ final_summary = f"{final_summary}\n{rename_lines}"+ except Exception as e:+ logger.debug("Curator rename summary build failed: %s", e, exc_info=True)+ return final_summary, llm_meta++def run_curator_review(on_summary: Optional[Callable[[str], None]] = None,synchronous: bool = False,@@ -1147,47 +1121,14 @@ def run_curator_review(def _llm_pass():# Snapshot skill state BEFORE the LLM pass so the report can diff.before_report = _safe_curated_report()- before_names = {r.get("name") for r in before_report if isinstance(r, dict)}+ before_names = set(_by_name(before_report))- if not consolidate:+ if consolidate:+ final_summary, llm_meta = _consolidation_pass(prefix, auto_summary, dry_run, before_names)+ else:# Prune-only run: record it and write a report, but never fork.final_summary = f"{prefix}{auto_summary}; llm: skipped (consolidation off)"llm_meta = _llm_meta("skipped (consolidation off)")- else:- try:- candidate_list = _render_candidate_list()- if "No agent-created skills" in candidate_list:- final_summary = f"{prefix}{auto_summary}; llm: skipped (no candidates)"- llm_meta = _llm_meta("skipped (no candidates)")- else:- # With prune-builtins on, bundled skills are candidates too:- # relax hard rule #1 for them (archive only; hub stays off-limits).- builtins_note = CURATOR_PRUNE_BUILTINS_NOTE if get_prune_builtins() else ""- prompt = f"{CURATOR_REVIEW_PROMPT}{builtins_note}\n\n{candidate_list}"- if dry_run:- prompt = f"{CURATOR_DRY_RUN_BANNER}\n\n{prompt}"- llm_meta = _run_llm_review(prompt)- final_summary = (- f"{prefix}{auto_summary}; llm: {llm_meta.get('summary', 'no change')}"- )- except Exception as e:- logger.debug("Curator LLM pass failed: %s", e, exc_info=True)- final_summary = f"{prefix}{auto_summary}; llm: error ({e})"- llm_meta = _llm_meta(f"error ({e})", str(e))-- # Append the rename map (`old-name → umbrella`) so users needn't dig- # into REPORT.md. Best-effort: never block the run on formatting.- try:- rename_lines = _build_rename_summary(- before_names=before_names,- after_report=skill_usage.curated_report(),- tool_calls=llm_meta.get("tool_calls", []) or [],- model_final=llm_meta.get("final", "") or "",- )- if rename_lines:- final_summary = f"{final_summary}\n{rename_lines}"- except Exception as e:- logger.debug("Curator rename summary build failed: %s", e, exc_info=True)elapsed = (datetime.now(timezone.utc) - start).total_seconds()state2 = load_state()@@ -1195,17 +1136,11 @@ def run_curator_review(state2["last_run_summary"] = final_summary# Per-run report, best-effort; path recorded for `hermes curator status`.- after_report = _safe_curated_report()try:report_path = _write_run_report(- started_at=start,- elapsed_seconds=elapsed,- auto_counts=counts,- auto_summary=auto_summary,- before_report=before_report,- before_names=before_names,- after_report=after_report,- llm_meta=llm_meta,+ started_at=start, elapsed_seconds=elapsed, auto_counts=counts, auto_summary=auto_summary,+ before_report=before_report, before_names=before_names,+ after_report=_safe_curated_report(), llm_meta=llm_meta,)if report_path is not None:state2["last_report_path"] = str(report_path)@@ -1263,26 +1198,64 @@ def _resolve_review_runtime(cfg: Dict[str, Any]) -> _ReviewRuntimeBinding:Non-empty slot ``api_key``/``base_url`` are returned as explicit overrides so``resolve_runtime_provider`` doesn't reuse the main chat credential chain."""- _cur_task = _subdict(cfg, "auxiliary", "curator")- _task_provider = (_cur_task.get("provider") or "").strip() or None- _task_model = (_cur_task.get("model") or "").strip() or None- if _task_provider and _task_provider != "auto" and _task_model:- return _slot_binding(_task_provider, _task_model, _cur_task)-- _legacy = _subdict(cfg, "curator", "auxiliary")- _legacy_provider = _legacy.get("provider") or None- _legacy_model = _legacy.get("model") or None- if _legacy_provider and _legacy_model:+ task = _subdict(cfg, "auxiliary", "curator")+ task_provider = (task.get("provider") or "").strip() or None+ task_model = (task.get("model") or "").strip() or None+ if task_provider and task_provider != "auto" and task_model:+ return _slot_binding(task_provider, task_model, task)++ legacy = _subdict(cfg, "curator", "auxiliary")+ if legacy.get("provider") and legacy.get("model"):logger.info("curator: using deprecated curator.auxiliary.{provider,model} ""config — please migrate to auxiliary.curator.{provider,model}")- return _slot_binding(str(_legacy_provider), str(_legacy_model), _legacy)+ return _slot_binding(str(legacy["provider"]), str(legacy["model"]), legacy)- _main = _subdict(cfg, "model")- return _ReviewRuntimeBinding(- _main.get("provider") or "auto", _main.get("default") or _main.get("model") or "", None, None, {},- )+ main = _subdict(cfg, "model")+ return _ReviewRuntimeBinding(main.get("provider") or "auto", main.get("default") or main.get("model") or "", None, None, {})+++def _resolve_review_provider() -> tuple:+ """``(runtime_provider, model_name, provider_name, request_overrides)`` resolved+ the way the CLI does: AIAgent() without explicit provider/model hits an+ auto-resolution path that fails for OAuth-only providers and pooled+ credentials (HTTP 400 "No models provided"). Never raises."""+ rp: Dict[str, Any] = {}+ overrides: Dict[str, Any] = {}+ provider, model_name = None, ""+ try:+ from hermes_cli.config import load_config_readonly+ from hermes_cli.runtime_provider import resolve_runtime_provider+ binding = _resolve_review_runtime(load_config_readonly())+ model_name = binding.model+ rp = resolve_runtime_provider(+ requested=binding.provider, target_model=binding.model,+ explicit_api_key=binding.explicit_api_key, explicit_base_url=binding.explicit_base_url,+ )+ provider = rp.get("provider") or binding.provider+ overrides = _merge_request_overrides(rp.get("request_overrides"), binding.request_overrides.get("extra_body"))+ if isinstance(rp.get("model"), str) and rp["model"].strip():+ model_name = rp["model"].strip()+ except Exception as e:+ logger.debug("Curator provider resolution failed: %s", e, exc_info=True)+ return rp, model_name, provider, overrides+++def _collect_tool_calls(review_agent: Any) -> List[Dict[str, Any]]:+ """[{name, arguments}] from the fork's session; arguments truncated to 400+ chars so a giant skill_manage create doesn't blow up the report."""+ calls: List[Dict[str, Any]] = []+ for msg in getattr(review_agent, "_session_messages", []) or []:+ for tc in (msg.get("tool_calls") or []) if isinstance(msg, dict) else []:+ if not isinstance(tc, dict):+ continue+ fn = tc.get("function") or {}+ args_raw = fn.get("arguments") or ""+ if isinstance(args_raw, str) and len(args_raw) > 400:+ args_raw = args_raw[:400] + "…"+ calls.append({"name": fn.get("name") or "", "arguments": args_raw})+ return callsdef _run_llm_review(prompt: str) -> Dict[str, Any]:@@ -1297,54 +1270,28 @@ def _run_llm_review(prompt: str) -> Dict[str, Any]:result_meta["error"] = result_meta["summary"] = f"AIAgent import failed: {e}"return result_meta- # Resolve provider + model the same way the CLI does: AIAgent() without- # explicit provider/model hits an auto-resolution path that fails for- # OAuth-only providers and pooled credentials (HTTP 400 "No models provided").- _rp: Dict[str, Any] = {}- _request_overrides: Dict[str, Any] = {}- _resolved_provider, _model_name = None, ""- try:- from hermes_cli.config import load_config_readonly- from hermes_cli.runtime_provider import resolve_runtime_provider- _binding = _resolve_review_runtime(load_config_readonly())- _model_name = _binding.model- _rp = resolve_runtime_provider(- requested=_binding.provider,- target_model=_binding.model,- explicit_api_key=_binding.explicit_api_key,- explicit_base_url=_binding.explicit_base_url,- )- _resolved_provider = _rp.get("provider") or _binding.provider- _request_overrides = _merge_request_overrides(- _rp.get("request_overrides"),- _binding.request_overrides.get("extra_body"),- )- if isinstance(_rp.get("model"), str) and _rp["model"].strip():- _model_name = _rp["model"].strip()- except Exception as e:- logger.debug("Curator provider resolution failed: %s", e, exc_info=True)-- result_meta["model"] = _model_name- result_meta["provider"] = _resolved_provider or ""+ rp, model_name, provider, request_overrides = _resolve_review_provider()+ result_meta["model"] = model_name+ result_meta["provider"] = provider or ""review_agent = Nonetry:- _agent_kwargs: Dict[str, Any] = {}- if isinstance(_rp.get("max_output_tokens"), int):- _agent_kwargs["max_tokens"] = _rp["max_output_tokens"]- _acp_command = _rp.get("command")- if isinstance(_acp_command, str) and _acp_command:- _agent_kwargs["acp_command"] = _acp_command- _agent_kwargs["acp_args"] = list(_rp.get("args") or [])+ agent_kwargs: Dict[str, Any] = {}+ if isinstance(rp.get("max_output_tokens"), int):+ agent_kwargs["max_tokens"] = rp["max_output_tokens"]+ acp_command = rp.get("command")+ if isinstance(acp_command, str) and acp_command:+ agent_kwargs["acp_command"] = acp_command+ agent_kwargs["acp_args"] = list(rp.get("args") or [])review_agent = AIAgent(- model=_model_name,- provider=_resolved_provider,- api_key=_rp.get("api_key"),- base_url=_rp.get("base_url"),- api_mode=_rp.get("api_mode"),- credential_pool=_rp.get("credential_pool"),- request_overrides=_request_overrides,- **_agent_kwargs,+ model=model_name,+ provider=provider,+ api_key=rp.get("api_key"),+ base_url=rp.get("base_url"),+ api_mode=rp.get("api_mode"),+ credential_pool=rp.get("credential_pool"),+ request_overrides=request_overrides,+ **agent_kwargs,# No ``terminal``: a shell mv/cp/rm under the skills tree writes bytes# with NO ledger entry, so rollback would restore a hollow skill. Every# mutation goes through ledgered skill_manage; dropping the toolset@@ -1366,28 +1313,14 @@ def _run_llm_review(prompt: str) -> Dict[str, Any]:review_agent._memory_write_origin = "background_review"# Silence the fork's tool-call chatter (CLI synchronous foreground runs).- with open(os.devnull, "w", encoding="utf-8") as _devnull, \- contextlib.redirect_stdout(_devnull), \- contextlib.redirect_stderr(_devnull):+ with open(os.devnull, "w", encoding="utf-8") as devnull, \+ contextlib.redirect_stdout(devnull), contextlib.redirect_stderr(devnull):conv_result = review_agent.run_conversation(user_message=prompt)final = str(conv_result.get("final_response") or "").strip() if isinstance(conv_result, dict) else ""result_meta["final"] = finalresult_meta["summary"] = (final[:240] + "…") if len(final) > 240 else (final or "no change")-- # Collect tool calls for the report; truncate arguments so a giant- # skill_manage create doesn't blow up the report.- _calls: List[Dict[str, Any]] = []- for msg in getattr(review_agent, "_session_messages", []) or []:- for tc in (msg.get("tool_calls") or []) if isinstance(msg, dict) else []:- if not isinstance(tc, dict):- continue- fn = tc.get("function") or {}- args_raw = fn.get("arguments") or ""- if isinstance(args_raw, str) and len(args_raw) > 400:- args_raw = args_raw[:400] + "…"- _calls.append({"name": fn.get("name") or "", "arguments": args_raw})- result_meta["tool_calls"] = _calls+ result_meta["tool_calls"] = _collect_tool_calls(review_agent)except Exception as e:result_meta["error"] = result_meta["summary"] = f"error: {e}"finally:diff --git a/agent/curator_backup.py b/agent/curator_backup.pyindex 25e88c82550..9e57a9c1526 100644--- a/agent/curator_backup.py+++ b/agent/curator_backup.py@@ -3,18 +3,14 @@Before any mutating curator pass, ``~/.hermes/skills/`` is tar.gz'd under``~/.hermes/skills/.curator_backups/<utc-iso>/`` with a ``manifest.json``.Rollback first snapshots the CURRENT tree (so it is itself undoable), then-extracts the chosen snapshot into place.--Excluded: ``.curator_backups/`` (would recurse), ``.hub/`` (hub-managed) and-``.git/`` (repository metadata — managed by git, not the curator).-Included: every skill dir, ``.usage.json``, ``.archive/``, ``.curator_state``-(so rollback also restores last-run-at and the curator doesn't re-fire),-``.bundled_manifest`` and ``.curator_suppressed``.+extracts the chosen snapshot into place. Excluded: ``.curator_backups/``,+``.hub/`` (hub-managed), ``.git/``. Included: skill dirs, ``.usage.json``,+``.archive/``, ``.curator_state`` (so rollback also restores last-run-at and the+curator doesn't re-fire), ``.bundled_manifest``, ``.curator_suppressed``.Each snapshot also copies ``~/.hermes/cron/jobs.json`` as ``cron-jobs.json``:the consolidation pass rewrites cron ``skills``/``skill`` references in place,-so without it rolling back the skills tree would leave jobs pointing at-umbrellas. Rollback restores only those two fields; the rest is live state.+so rollback restores those two fields (only) — the rest is live state."""from __future__ import annotations@@ -54,6 +50,7 @@ _ID_RE = re.compile(r"^\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}Z(-\d{2})?$")CRON_JOBS_FILENAME = "cron-jobs.json"_ARCHIVE_NAME = "skills.tar.gz"+_STAGING_PREFIX = ".rollback-staging-"def _skills_dir() -> Path:@@ -95,7 +92,6 @@ def _backup_cron_jobs_into(dest: Path) -> Dict[str, Any]:if jobs is not None:info["jobs_count"] = len(jobs)except (json.JSONDecodeError, TypeError):- info["jobs_count"] = 0info["parse_warning"] = "jobs.json was not valid JSON at snapshot time"try:(dest / CRON_JOBS_FILENAME).write_text(raw, encoding="utf-8")@@ -160,6 +156,19 @@ def _rmtree_quiet(path: Path) -> None:shutil.rmtree(path, ignore_errors=True)+def _mkdir(path: Path, what: str, *, exist_ok: bool) -> bool:+ try:+ path.mkdir(parents=True, exist_ok=exist_ok)+ return True+ except OSError as e:+ logger.debug("Failed to create %s %s: %s", what, path, e)+ return False+++def _tar_filter(tarinfo: tarfile.TarInfo) -> Optional[tarfile.TarInfo]:+ return None if any(p in _EXCLUDE_TOP_LEVEL for p in Path(tarinfo.name).parts) else tarinfo++def snapshot_skills(reason: str = "manual", *, protect_ids: Optional[Set[str]] = None) -> Optional[Path]:"""Create a tar.gz snapshot of ``~/.hermes/skills/`` and prune old ones.Returns the snapshot dir, or None when skipped (disabled, skills dir missing,@@ -168,47 +177,31 @@ def snapshot_skills(reason: str = "manual", *, protect_ids: Optional[Set[str]] =if not is_enabled():logger.debug("Curator backup disabled by config; skipping snapshot")return None-skills = _skills_dir()if not skills.exists():logger.debug("No ~/.hermes/skills/ directory — nothing to back up")return None-backups = _backups_dir()- try:- backups.mkdir(parents=True, exist_ok=True)- except OSError as e:- logger.debug("Failed to create backups dir %s: %s", backups, e)+ if not _mkdir(backups, "backups dir", exist_ok=True):return None# Two curator runs in the same second must not clobber each other.- base_id = _utc_id()- snap_id = base_id+ base_id = snap_id = _utc_id()counter = 1while (backups / snap_id).exists():snap_id = f"{base_id}-{counter:02d}"counter += 1-dest = backups / snap_id- try:- dest.mkdir(parents=True, exist_ok=False)- except OSError as e:- logger.debug("Failed to create snapshot dir %s: %s", dest, e)+ if not _mkdir(dest, "snapshot dir", exist_ok=False):return Nonearchive = dest / _ARCHIVE_NAME-- def _tar_filter(tarinfo: tarfile.TarInfo) -> Optional[tarfile.TarInfo]:- parts = Path(tarinfo.name).parts- return None if any(p in _EXCLUDE_TOP_LEVEL for p in parts) else tarinfo-try:with tarfile.open(archive, "w:gz", compresslevel=6) as tf:for entry in sorted(skills.iterdir()):- if entry.name in _EXCLUDE_TOP_LEVEL:- continue- # arcname relative to skills/ so extraction drops back in cleanly.- tf.add(str(entry), arcname=entry.name, recursive=True, filter=_tar_filter)+ if entry.name not in _EXCLUDE_TOP_LEVEL:+ # arcname relative to skills/ so extraction drops back in cleanly.+ tf.add(str(entry), arcname=entry.name, recursive=True, filter=_tar_filter)# Cron capture is additive and never fails the snapshot; the manifest# records whether it happened so rollback can say "no cron data"._write_manifest(dest, reason, archive, _count_skill_files(skills), _backup_cron_jobs_into(dest))@@ -232,23 +225,18 @@ def _prune_old(keep: int, protect: Optional[Set[str]] = None) -> List[str]:if not backups.exists():return []dirs = [c for c in backups.iterdir() if c.is_dir()]- stale_staging = [c for c in dirs if c.name.startswith(".rollback-staging-")]# Newest first (lexicographic works because the id is UTC ISO).entries = sorted((c for c in dirs if _ID_RE.match(c.name)), key=lambda c: c.name, reverse=True)+ doomed = [(p, "prune") for p in entries[keep:] if p.name not in protect]+ doomed += [(p, "clean stale staging dir") for p in dirs if p.name.startswith(_STAGING_PREFIX)]deleted: List[str] = []- for path in entries[keep:]:- if path.name in protect:- continue- try:- shutil.rmtree(path)- deleted.append(path.name)- except OSError as e:- logger.debug("Failed to prune %s: %s", path, e)- for path in stale_staging:+ for path, what in doomed:try:shutil.rmtree(path)+ if what == "prune":+ deleted.append(path.name)except OSError as e:- logger.debug("Failed to clean stale staging dir %s: %s", path, e)+ logger.debug("Failed to %s %s: %s", what, path, e)return deleted@@ -310,7 +298,6 @@ def _restore_cron_skill_links(snapshot_dir: Path) -> Dict[str, Any]:if not backup_file.exists():report["error"] = f"snapshot has no {CRON_JOBS_FILENAME}"return report-try:backup_jobs = _jobs_list(json.loads(backup_file.read_text(encoding="utf-8")))except (OSError, json.JSONDecodeError) as e:@@ -326,11 +313,9 @@ def _restore_cron_skill_links(snapshot_dir: Path) -> Dict[str, Any]:for job in backup_jobsif isinstance(job, dict) and isinstance(job.get("id"), str) and job.get("id")}-if not backup_by_id:report["attempted"] = True # we tried but there was nothing to doreturn report-try:from cron.jobs import load_jobs, save_jobs, _jobs_lockexcept ImportError as e:@@ -344,9 +329,7 @@ def _restore_cron_skill_links(snapshot_dir: Path) -> Dict[str, Any]:changed = Falselive_ids = set()for live in live_jobs:- if not isinstance(live, dict):- continue- jid = live.get("id")+ jid = live.get("id") if isinstance(live, dict) else Noneif not isinstance(jid, str) or not jid:continuelive_ids.add(jid)@@ -377,7 +360,6 @@ def _restore_cron_skill_links(snapshot_dir: Path) -> Dict[str, Any]:except Exception as e: # noqa: BLE001 — rollback must not die mid-restorelogger.debug("Cron skill-link restore failed: %s", e, exc_info=True)report["error"] = f"restore failed mid-flight: {e}"-return report@@ -407,16 +389,10 @@ def _restore_excluded_subtrees(staged: Path, skills: Path) -> None:logger.debug("Could not restore excluded entry %s: %s", src, e)for dirpath, dirnames, filenames in os.walk(staged):- keep = []- for name in dirnames:- if name in _EXCLUDE_TOP_LEVEL:- _carry(Path(dirpath) / name)- else:- keep.append(name)- dirnames[:] = keep- for name in filenames:+ for name in [*dirnames, *filenames]:if name in _EXCLUDE_TOP_LEVEL:_carry(Path(dirpath) / name)+ dirnames[:] = [d for d in dirnames if d not in _EXCLUDE_TOP_LEVEL]def _unstage(moved: List[Tuple[Path, Path]]) -> List[str]:@@ -434,6 +410,34 @@ def _unstage(moved: List[Tuple[Path, Path]]) -> List[str]:return failed+def _extract_snapshot(archive: Path, skills: Path) -> None:+ """Extract into *skills*; raises ``tarfile.TarError`` on unsafe member paths."""+ with tarfile.open(archive, "r:gz") as tf:+ # Reject absolute paths and ".." defensively; Python 3.12+ also+ # gets filter='data', older interpreters fall back unfiltered.+ for member in tf.getmembers():+ if member.name.startswith("/") or ".." in Path(member.name).parts:+ raise tarfile.TarError(f"refusing to extract unsafe path: {member.name!r}")+ try:+ tf.extractall(str(skills), filter="data") # type: ignore[call-arg]+ except TypeError:+ tf.extractall(str(skills)) # Python < 3.12 — no filter kwarg+++def _cron_summary(cron_report: Dict[str, Any]) -> Optional[str]:+ if not cron_report.get("attempted"):+ return None+ if cron_report.get("error"):+ return f"cron links: error — {cron_report['error']}"+ # (attempted with nothing matched — empty snapshot or no overlapping ids — says nothing)+ parts = [f"{n} {label}" for n, label in (+ (len(cron_report.get("restored") or []), "job(s) had skill links restored"),+ (len(cron_report.get("skipped_missing") or []), "backed-up job(s) no longer exist (skipped)"),+ (cron_report.get("unchanged", 0), "already matched"),+ ) if n]+ return "cron links: " + ", ".join(parts) if parts else None++def rollback(backup_id: Optional[str] = None) -> Tuple[bool, str, Optional[Path]]:"""Restore ``~/.hermes/skills/`` from a snapshot (explicit id or newest):safety-snapshot the CURRENT tree; stage current top-level entries; extract;@@ -459,10 +463,7 @@ def rollback(backup_id: Optional[str] = None) -> Tuple[bool, str, Optional[Path]# Safety snapshot FIRST; bail if it fails, else a failed extract could leave# the user with no skills. Protect the target from this snapshot's prune step.try:- safety_snapshot = snapshot_skills(- reason=f"pre-rollback to {target.name}",- protect_ids={target.name},- )+ safety_snapshot = snapshot_skills(reason=f"pre-rollback to {target.name}", protect_ids={target.name})except Exception as e:return (False, f"pre-rollback safety snapshot failed: {e}", None)if safety_snapshot is None:@@ -475,7 +476,7 @@ def rollback(backup_id: Optional[str] = None) -> Tuple[bool, str, Optional[Path]# Stage current entries so the extract lands in an empty tree; the safety# snapshot above (not staging) is the user-facing undo handle.- staged = backups / f".rollback-staging-{_utc_id()}"+ staged = backups / f"{_STAGING_PREFIX}{_utc_id()}"try:staged.mkdir(parents=True, exist_ok=False)except OSError as e:@@ -484,38 +485,27 @@ def rollback(backup_id: Optional[str] = None) -> Tuple[bool, str, Optional[Path]moved: List[Tuple[Path, Path]] = []try:for entry in list(skills.iterdir()):- if entry.name in _EXCLUDE_TOP_LEVEL:- continue- dest = staged / entry.name- shutil.move(str(entry), str(dest))- moved.append((entry, dest))+ if entry.name not in _EXCLUDE_TOP_LEVEL:+ dest = staged / entry.name+ shutil.move(str(entry), str(dest))+ moved.append((entry, dest))except OSError as e:_unstage(moved)_rmtree_quiet(staged)return (False, f"failed to stage current skills: {e}", None)try:- with tarfile.open(archive, "r:gz") as tf:- # Reject absolute paths and ".." defensively; Python 3.12+ also- # gets filter='data', older interpreters fall back unfiltered.- for member in tf.getmembers():- if member.name.startswith("/") or ".." in Path(member.name).parts:- raise tarfile.TarError(f"refusing to extract unsafe path: {member.name!r}")- try:- tf.extractall(str(skills), filter="data") # type: ignore[call-arg]- except TypeError:- tf.extractall(str(skills)) # Python < 3.12 — no filter kwarg+ _extract_snapshot(archive, skills)except (OSError, tarfile.TarError) as e:# A partial extract can leave entries the original tree never had;# drop those first or the "restored" tree is skills + a slice of snapshot.staged_names = {orig.name for orig, _ in moved}for entry in list(skills.iterdir()):- if entry.name in _EXCLUDE_TOP_LEVEL or entry.name in staged_names:- continue- try:- _remove_entry(entry)- except OSError:- pass+ if entry.name not in _EXCLUDE_TOP_LEVEL and entry.name not in staged_names:+ try:+ _remove_entry(entry)+ except OSError:+ passunrestored = _unstage(moved)if unrestored:# Don't claim a clean restore; keep the staging dir for hand recovery.@@ -537,24 +527,9 @@ def rollback(backup_id: Optional[str] = None) -> Tuple[bool, str, Optional[Path]# Cron reconciliation failures don't fail the rollback — the skills tree# (the main guarantee) is already restored.cron_report = _restore_cron_skill_links(target)-- summary_bits = [f"restored from snapshot {target.name}"]- if cron_report.get("attempted"):- if cron_report.get("error"):- summary_bits.append(f"cron links: error — {cron_report['error']}")- else:- # (attempted with nothing matched — empty snapshot or no overlapping ids — says nothing)- parts = [f"{n} {label}" for n, label in (- (len(cron_report.get("restored") or []), "job(s) had skill links restored"),- (len(cron_report.get("skipped_missing") or []), "backed-up job(s) no longer exist (skipped)"),- (cron_report.get("unchanged", 0), "already matched"),- ) if n]- if parts:- summary_bits.append("cron links: " + ", ".join(parts))-- logger.info("Curator rollback: restored from %s (cron_report=%s)",- target.name, cron_report)- return (True, "; ".join(summary_bits), target)+ summary_bits = [f"restored from snapshot {target.name}", _cron_summary(cron_report)]+ logger.info("Curator rollback: restored from %s (cron_report=%s)", target.name, cron_report)+ return (True, "; ".join(filter(None, summary_bits)), target)# --- Human-readable summary for CLI ---