Skip to content

The agent that grows with you

refactor(agent/curator): merge single-use helpers, collapse report/state assembly; class-form NamedTuples restored

Tekniumfa986633d9a4
diff --git a/agent/curator.py b/agent/curator.py
index 3b96ec7624d..ce1b9507efa 100644
--- a/agent/curator.py
+++ b/agent/curator.py
@@ -13,6 +13,7 @@ auxiliary client and never touches the main session's prompt cache.
from __future__ import annotations
+import contextlib
import json
import logging
import os
@@ -50,10 +51,8 @@ def load_state() -> Dict[str, Any]:
"last_run_summary_shown_at": None, "last_report_path": None, "paused": False, "run_count": 0,
}
path = _state_file()
- if not path.exists():
- return base
try:
- data = json.loads(path.read_text(encoding="utf-8"))
+ data = json.loads(path.read_text(encoding="utf-8")) if path.exists() else {}
except (OSError, json.JSONDecodeError) as e:
logger.debug("Failed to read curator state: %s", e)
return base
@@ -69,10 +68,7 @@ def save_state(data: Dict[str, Any]) -> None:
logger.debug("Failed to save curator state: %s", e, exc_info=True)
-def set_paused(paused: bool) -> None:
- state = load_state()
- state["paused"] = bool(paused)
- save_state(state)
+def set_paused(paused: bool) -> None: save_state({**load_state(), "paused": bool(paused)})
def is_paused() -> bool:
@@ -110,8 +106,7 @@ def _config_number(key: str, default, cast):
return default
-def is_enabled() -> bool:
- """Default ON when no config says otherwise."""
+def is_enabled() -> bool: # default ON when no config says otherwise
return bool(_load_config().get("enabled", True))
@@ -132,14 +127,12 @@ def get_archive_after_days() -> int:
def get_prune_builtins() -> bool:
- """Bundled built-ins are curation candidates (ON by default); a suppression list
- keeps them archived across `hermes update` re-seeds. Hub 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 are never pruned."""
return bool(_load_config().get("prune_builtins", True))
def get_consolidate() -> bool:
- """LLM consolidation pass — OFF by default (prune only, 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))
@@ -153,10 +146,9 @@ def _parse_iso(ts: Optional[str]) -> Optional[datetime]:
def should_run_now(now: Optional[datetime] = None) -> bool:
- """Gates: curator.enabled, not paused, ``last_run_at`` present AND older than
- interval_hours. First observation seeds ``last_run_at`` to now and defers one
- interval, so a fresh install/update never mutates the library on its first
- tick. ``hermes curator run`` bypasses this; the idle check is the caller's."""
+ """Gates: curator.enabled, not paused, ``last_run_at`` present AND older than interval_hours. First observation seeds
+ ``last_run_at`` to now and defers one interval, so a fresh install/update never mutates the library on its first tick.
+ ``hermes curator run`` bypasses this; the idle check is the caller's."""
if not is_enabled() or is_paused():
return False
state = load_state()
@@ -165,15 +157,11 @@ def should_run_now(now: Optional[datetime] = None) -> bool:
if last is None:
try:
state["last_run_at"] = now.isoformat()
- state["last_run_summary"] = (
- "deferred first run — curator seeded, will run after one "
- "interval; use `hermes curator run --dry-run` to preview now"
- )
+ state["last_run_summary"] = "deferred first run — curator seeded, will run after one interval; use `hermes curator run --dry-run` to preview now"
save_state(state)
except Exception as e: # pragma: no cover — best-effort persistence
logger.debug("Failed to seed curator last_run_at: %s", e)
return False
-
if last.tzinfo is None:
last = last.replace(tzinfo=timezone.utc)
return (now - last) >= timedelta(hours=get_interval_hours())
@@ -182,8 +170,7 @@ 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 yields an empty set, never a 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 _refs
return _refs()
@@ -193,8 +180,7 @@ def _cron_referenced_skills() -> Set[str]:
def _archive_as_curator(_u, name: str) -> bool:
- """Archive via skill_usage with the ledger actor tagged 'curator', so the
- ledger entry reads as an autonomous transition, not a foreground call."""
+ """Archive via skill_usage with the ledger actor tagged 'curator', so the ledger entry reads as an autonomous transition, not a foreground call."""
try:
from tools.skill_ledger import reset_ledger_actor, set_ledger_actor
tok = set_ledger_actor("curator")
@@ -204,17 +190,14 @@ def _archive_as_curator(_u, name: str) -> bool:
return _u.archive_skill(name)[0]
finally:
if tok is not None:
- try:
+ with contextlib.suppress(Exception):
reset_ledger_actor(tok)
- except Exception:
- pass
def apply_automatic_transitions(now: Optional[datetime] = None) -> Dict[str, int]:
- """Move every curator-managed skill between active/stale/archived based on
- its latest real activity; pinned skills are never touched. Built-ins are
- seeded with a baseline record on first sight so their inactivity clock
- starts NOW, not at epoch. Returns a counter dict."""
+ """Move every curator-managed skill between active/stale/archived based on its latest real activity; pinned skills are
+ never touched. Built-ins are seeded with a baseline record on first sight so their inactivity clock starts NOW, not at epoch.
+ Returns a counter dict."""
from tools import skill_usage as _u
now = now or datetime.now(timezone.utc)
@@ -223,7 +206,6 @@ def apply_automatic_transitions(now: Optional[datetime] = None) -> Dict[str, int
# 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:
@@ -240,20 +222,17 @@ def apply_automatic_transitions(now: Optional[datetime] = None) -> Dict[str, int
_u.seed_record_if_missing(name)
counts["seeded"] += 1
continue
-
# Never-active skills anchor on created_at so they don't self-archive.
anchor = _parse_iso(row.get("last_activity_at")) or _parse_iso(row.get("created_at")) or now
if 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.
if int(row.get("use_count", 0) or 0) == 0 and anchor > stale_cutoff:
if current == _u.STATE_STALE:
_set(name, _u.STATE_ACTIVE, "reactivated")
continue
-
if anchor <= archive_cutoff and current != _u.STATE_ARCHIVED:
if _archive_as_curator(_u, name):
counts["archived"] += 1
@@ -261,7 +240,6 @@ def apply_automatic_transitions(now: Optional[datetime] = None) -> Dict[str, int
_set(name, _u.STATE_STALE, "marked_stale")
elif anchor > stale_cutoff and current == _u.STATE_STALE:
_set(name, _u.STATE_ACTIVE, "reactivated") # used again after going stale
-
return counts
@@ -471,13 +449,10 @@ CURATOR_PRUNE_BUILTINS_NOTE = (
"the user explicitly restores it."
)
-
-
# --- Per-run reports — {YYYYMMDD-HHMMSS}/run.json + REPORT.md under logs/curator/ ---
def _reports_root() -> Path:
- """``~/.hermes/logs/curator/`` (telemetry next to agent.log, not under skills/).
- mkdir'd here too so gateway-only / bare-library entry paths work."""
+ """``~/.hermes/logs/curator/`` (telemetry next to agent.log, not under skills/). mkdir'd here too so gateway-only / bare-library entry paths work."""
root = get_hermes_home() / "logs" / "curator"
try:
root.mkdir(parents=True, exist_ok=True)
@@ -487,27 +462,20 @@ def _reports_root() -> Path:
def _needle_in_path_component(needle: str, path: str) -> bool:
- """True if *needle* equals a complete filename stem or directory name in
- *path* — so "api" does not match "references/api-design.md". Hyphens and
- underscores are normalised ("open-webui-setup" matches "open_webui_setup.md")."""
+ """True if *needle* equals a complete filename stem or directory name in *path* — so "api" does not match
+ "references/api-design.md". Hyphens and underscores are normalised ("open-webui-setup" matches "open_webui_setup.md")."""
norm_needle = needle.replace("-", "_")
- return any(
- part and part.rsplit(".", 1)[0].replace("-", "_") == norm_needle
- for part in path.replace("\\", "/").split("/")
- )
+ return any(part and part.rsplit(".", 1)[0].replace("-", "_") == norm_needle for part in path.replace("\\", "/").split("/"))
def _skill_manage_args(tc: Any, *, raw_fallback: bool) -> Optional[Dict[str, Any]]:
- """Parsed arguments of a ``skill_manage`` tool call (JSON string or dict), or
- None to skip. With *raw_fallback*, a malformed string yields ``{"_raw": raw}``
- so substring matching still catches the common case."""
+ """Parsed arguments of a ``skill_manage`` tool call (JSON string or dict), or None to skip. With *raw_fallback*,
+ a malformed string yields ``{"_raw": raw}`` so substring matching still catches the common case."""
if not isinstance(tc, dict) or tc.get("name") != "skill_manage":
return None
raw = tc.get("arguments") or ""
- if isinstance(raw, dict):
- return raw
if not isinstance(raw, str):
- return None
+ return raw if isinstance(raw, dict) else None
try:
args = json.loads(raw)
except Exception:
@@ -515,104 +483,80 @@ def _skill_manage_args(tc: Any, *, raw_fallback: bool) -> Optional[Dict[str, Any
return args if isinstance(args, dict) else None
-_REFERENCE_FIELDS = ("file_path", "file_content", "content", "new_string", "_raw")
-
-
def _find_reference(args: Dict[str, Any], needles: Set[str]) -> Optional[str]:
- """First argument value (in ``_REFERENCE_FIELDS`` order) that references
- one of *needles*. ``file_path`` must match a whole path component; content
- fields match on word boundaries so "test" does not match "latest"."""
- for key in _REFERENCE_FIELDS:
+ """First argument value (file_path, file_content, content, new_string, _raw — in that order) that references one of
+ *needles*. ``file_path`` must match a whole path component; content fields match on word boundaries so "test" does not match "latest"."""
+ for key in ("file_path", "file_content", "content", "new_string", "_raw"):
hay = args.get(key)
- if not isinstance(hay, str):
- 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
+ if isinstance(hay, str) and any(
+ (_needle_in_path_component(n, hay) if key == "file_path" else re.search(rf'\b{re.escape(n)}\b', hay)) for n in filter(None, needles)
+ ):
+ return hay
return None
def _classify_removed_skills(
removed: List[str], added: List[str], after_names: Set[str], tool_calls: List[Dict[str, Any]],
) -> Dict[str, List[Dict[str, Any]]]:
- """Split ``removed`` into consolidated vs pruned. Heuristic: a ``skill_manage``
- call on a DIFFERENT, surviving-or-new skill whose file_path/content arguments
- reference the removed name is the "absorbed" signal; earliest match wins.
+ """Split ``removed`` into consolidated vs pruned. Heuristic: a ``skill_manage`` call on a DIFFERENT, surviving-or-new
+ skill whose file_path/content arguments reference the removed name is the "absorbed" signal; earliest match wins.
Returns ``{"consolidated": [{name, into, evidence}], "pruned": [{name}]}``."""
consolidated: List[Dict[str, Any]] = []
pruned: List[Dict[str, Any]] = []
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 filter(None, removed):
needles = {name, name.replace("-", "_"), name.replace("_", "-")}
for args in parsed_calls:
target = args.get("name")
- # Calls on the removed skill itself, or on a skill that no longer
- # exists, are not consolidation evidence.
+ # Calls on the removed skill itself, or on a skill that no longer exists, are not consolidation evidence.
if not isinstance(target, str) or not target or target == name or target not in destinations:
continue
hay = _find_reference(args, needles)
if hay is not None:
- consolidated.append({"name": name, "into": target, "evidence": (
- f"skill_manage action={args.get('action', '?')} on '{target}' referenced '{name}' in {hay[:80]}"
- )})
+ consolidated.append({"name": name, "into": target, "evidence":
+ f"skill_manage action={args.get('action', '?')} on '{target}' referenced '{name}' in {hay[:80]}"})
break
else:
pruned.append({"name": name})
-
return {"consolidated": consolidated, "pruned": pruned}
-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]]]:
- """Extract the required fenced ```yaml block (``consolidations:`` /
- ``prunings:`` lists) from the curator's final response. Tolerant: missing
- block or malformed YAML → empty lists (caller falls back to the tool-call
- heuristic); a partial block returns what parsed.
- Returns ``{"consolidations": [{from, into, reason}], "prunings": [{name, reason}]}``."""
- out: Dict[str, List[Dict[str, str]]] = {"consolidations": [], "prunings": []}
- # Match ```yaml specifically so a code sample the model quoted elsewhere is
- # never mistaken for the summary.
+ """Extract the required fenced ```yaml block (``consolidations:`` / ``prunings:`` lists) from the curator's final
+ response. Tolerant: missing block or malformed YAML → empty lists (caller falls back to the tool-call heuristic); a partial
+ block returns what parsed. Returns ``{"consolidations": [{from, into, reason}], "prunings": [{name, reason}]}``."""
+ # 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) if isinstance(llm_final, str) else None
- if not match:
- return out
- try:
- import yaml # type: ignore
- data = yaml.safe_load(match.group(1))
- except Exception:
- return out
+ data = None
+ if match:
+ try:
+ import yaml # type: ignore
+ data = yaml.safe_load(match.group(1))
+ except Exception:
+ pass
if not isinstance(data, dict):
- return out
+ return {"consolidations": [], "prunings": []}
def _entries(key: str, *fields: str) -> List[Dict[str, str]]:
raw = data.get(key) or []
- cleaned = ({f: _clean_str(e.get(f)) for f in (*fields, "reason")} for e in raw if isinstance(e, dict)) if isinstance(raw, list) else ()
+ entries = (e for e in raw if isinstance(e, dict)) if isinstance(raw, list) else ()
+ cleaned = ({f: (v.strip() if isinstance((v := e.get(f)), str) else "") for f in (*fields, "reason")} for e in entries)
return [e for e in cleaned if all(e[f] for f in fields)]
- out["consolidations"] = _entries("consolidations", "from", "into")
- out["prunings"] = _entries("prunings", "name")
- return out
+ return {"consolidations": _entries("consolidations", "from", "into"), "prunings": _entries("prunings", "name")}
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 and
- substring heuristics). Returns ``{name: {"into": umbrella | "", "declared": True}}``;
- ``into == ""`` is an explicit prune. Deletes omitting ``absorbed_into`` are
- absent so the caller falls back to heuristic/YAML (older runs)."""
+ """Model-declared absorption targets from ``skill_manage(action='delete')`` calls — the authoritative classification
+ signal (beats YAML parsing and substring heuristics). Returns ``{name: {"into": umbrella | "", "declared": True}}``;
+ ``into == ""`` is an explicit prune. Deletes omitting ``absorbed_into`` are absent so the caller falls back to heuristic/YAML (older runs)."""
out: Dict[str, Dict[str, Any]] = {}
- for tc in tool_calls or []:
- args = _skill_manage_args(tc, raw_fallback=False)
- if args is None or args.get("action") != "delete":
- continue
- 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}
+ for args in (_skill_manage_args(tc, raw_fallback=False) for tc in tool_calls or []):
+ if args is not None and args.get("action") == "delete":
+ 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
@@ -622,24 +566,20 @@ def _reconcile_classification(
) -> Dict[str, List[Dict[str, Any]]]:
"""Merge heuristic (tool-call evidence) with the model's structured block.
First match wins; every removed skill lands in exactly one bucket:
- - ``absorbed_into`` declared at delete is authoritative: existing target →
- consolidated; ``""`` → pruned; missing target → fall through.
+ - ``absorbed_into`` declared at delete is authoritative: existing target → consolidated; ``""`` → pruned; missing target → fall through.
- Model-declared consolidation wins when its ``into`` is in ``destinations``.
- Model named a missing umbrella → heuristic's finding, else pruned.
- Heuristic-only consolidation kept, marked ``source="tool-call audit"``.
- - Otherwise pruned (model-declared, or no-evidence fallback).
- """
+ - Otherwise pruned (model-declared, or no-evidence fallback)."""
heur_cons = {e["name"]: e for e in heuristic.get("consolidated", [])}
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, 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:
@@ -652,19 +592,17 @@ def _reconcile_classification(
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)
+ _prune("absorbed_into=\"\" (model-declared prune)", (mp.get("reason") or "") if mp else "")
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"])
+ _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", ""))
else:
_prune("model" if mp else "no-evidence fallback", mp.get("reason", "") if mp else "")
-
return {"consolidated": consolidated, "pruned": pruned}
@@ -676,19 +614,14 @@ class _RunDiff(NamedTuple):
pruned: List[Dict[str, Any]]
-def _diff_and_classify(
- 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's
- YAML block carries intent + rationale, the tool-call heuristic audits for
- hallucinated umbrellas/omissions, per-delete ``absorbed_into`` beats both."""
- removed = sorted(before_names - after_names)
- added = sorted(after_names - before_names)
+def _diff_and_classify(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's YAML block carries intent + rationale,
+ the tool-call heuristic audits for hallucinated umbrellas/omissions, per-delete ``absorbed_into`` beats both."""
+ removed, added = sorted(before_names - after_names), sorted(after_names - before_names)
classification = _reconcile_classification(
removed=removed,
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),
+ model_block=_parse_structured_summary(model_final), destinations=set(after_names) | set(added),
absorbed_declarations=_extract_absorbed_into_declarations(tool_calls),
)
return _RunDiff(after_names, removed, added, classification["consolidated"], classification["pruned"])
@@ -698,18 +631,14 @@ 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,
-) -> str:
- """The "where did my skills go?" lines appended to the user-visible
- ``final_summary``; "" when nothing was archived. Capped at 10 entries so a
- big consolidation doesn't flood agent.log (full list is in REPORT.md); the
- pin hint appears only when a consolidation produced an umbrella."""
+def _build_rename_summary(*, 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 a big consolidation doesn't flood agent.log (full list is in REPORT.md); the pin hint
+ appears only when a consolidation produced an umbrella."""
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)
-
SHOW = 10
total = len(diff.consolidated) + len(diff.pruned)
entries = [f" • {e.get('name', '?')} → {e.get('into', '?')}" for e in diff.consolidated]
@@ -725,13 +654,10 @@ def _build_rename_summary(
def _rewrite_cron_refs(consolidated: List[Dict[str, Any]], pruned: List[Dict[str, Any]]) -> Dict[str, Any]:
- """Point cron jobs at the umbrella when the curator consolidated a skill they
- list — otherwise the scheduler fails to load it and the job runs without its
- instructions. Best-effort: a cron-module issue never breaks the curator."""
+ """Point cron jobs at the umbrella when the curator consolidated a skill they list — otherwise the scheduler fails to
+ load it and the job runs without its instructions. Best-effort: a cron-module issue never breaks the curator."""
try:
- consolidated_map = {
- e["name"]: e["into"] for e in consolidated if isinstance(e, dict) and e.get("name") and e.get("into")
- }
+ consolidated_map = {e["name"]: e["into"] for e in consolidated if isinstance(e, dict) and e.get("name") and e.get("into")}
pruned_names = [e["name"] for e in pruned if isinstance(e, dict) and e.get("name")]
if consolidated_map or pruned_names:
from cron.jobs import rewrite_skill_refs
@@ -742,87 +668,69 @@ def _rewrite_cron_refs(consolidated: List[Dict[str, Any]], pruned: List[Dict[str
return {"rewrites": [], "jobs_updated": 0, "jobs_scanned": 0, "error": str(e)}
-def _write_file(path: Path, label: str, render: Callable[[], str]) -> None:
- """Best-effort write; *render* runs inside the guard so a serialisation
- error is logged, not raised."""
+def _write_file(path: Path, label: str, render: Any) -> None:
+ """Best-effort write of *render()* (or the JSON dump of a non-callable payload);
+ rendering runs inside the guard so a serialisation error is logged, not raised."""
try:
- path.write_text(render(), encoding="utf-8")
+ path.write_text(render() if callable(render) else json.dumps(render, indent=2, ensure_ascii=False) + "\n", encoding="utf-8")
except Exception as e:
logger.debug("Curator %s write failed: %s", label, e)
-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")
+ """``logs/curator/{YYYYMMDD-HHMMSS}[-N]/`` (N disambiguates a crash-rerun in the same second); None when it can't be created."""
+ root, stamp = _reports_root(), 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)
+ return run_dir
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, elapsed_seconds: float, auto_counts: Dict[str, int], auto_summary: str,
before_report: List[Dict[str, Any]], before_names: Set[str], after_report: List[Dict[str, Any]], llm_meta: Dict[str, Any],
) -> Optional[Path]:
- """Write run.json + REPORT.md under logs/curator/{YYYYMMDD-HHMMSS}/. Returns
- the report dir, or None if it couldn't be created (reporting is best-effort)."""
+ """Write run.json + REPORT.md under logs/curator/{YYYYMMDD-HHMMSS}/. Returns the report dir, or None if it couldn't be created (best-effort)."""
run_dir = _new_run_dir(started_at)
if run_dir is None:
return None
-
tool_calls = llm_meta.get("tool_calls", []) 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 "")
-
states = ((n, (before_by_name.get(n) or {}).get("state"), (after_by_name.get(n) or {}).get("state")) for n in sorted(diff.after_names & before_names))
transitions = [{"name": n, "from": b, "to": a} for n, b, a in states if b and a and b != a]
-
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(), "duration_seconds": round(elapsed_seconds, 2),
- "model": llm_meta.get("model", ""), "provider": llm_meta.get("provider", ""),
- "auto_transitions": auto_counts,
+ "model": llm_meta.get("model", ""), "provider": llm_meta.get("provider", ""), "auto_transitions": auto_counts,
"counts": {
- "before": len(before_names), "after": len(diff.after_names),
- "delta": len(diff.after_names) - len(before_names),
+ "before": len(before_names), "after": len(diff.after_names), "delta": len(diff.after_names) - len(before_names),
"archived_this_run": len(diff.removed), "added_this_run": len(diff.added),
"consolidated_this_run": len(diff.consolidated), "pruned_this_run": len(diff.pruned),
- "state_transitions": len(transitions), "cron_jobs_rewritten": jobs_updated,
- "tool_calls_total": sum(tc_counts.values()),
+ "state_transitions": len(transitions), "cron_jobs_rewritten": jobs_updated, "tool_calls_total": sum(tc_counts.values()),
},
- "tool_call_counts": tc_counts,
- "archived": diff.removed, "consolidated": diff.consolidated, "pruned": diff.pruned,
- "pruned_names": [p["name"] for p in diff.pruned], "added": diff.added,
- "state_transitions": transitions, "cron_rewrites": cron_rewrites,
+ "tool_call_counts": tc_counts, "archived": diff.removed, "consolidated": diff.consolidated, "pruned": diff.pruned,
+ "pruned_names": [p["name"] for p in diff.pruned], "added": diff.added, "state_transitions": transitions, "cron_rewrites": cron_rewrites,
"llm_final": llm_meta.get("final", ""), "llm_summary": llm_meta.get("summary", ""),
"llm_error": llm_meta.get("error"), "tool_calls": llm_meta.get("tool_calls", []),
}
-
- _write_json(run_dir / "run.json", payload, "run.json")
+ _write_file(run_dir / "run.json", "run.json", payload)
_write_file(run_dir / "REPORT.md", "REPORT.md", lambda: _render_report_markdown(payload))
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")
+ _write_file(run_dir / "cron_rewrites.json", "cron_rewrites.json", cron_rewrites)
return run_dir
def _reason_suffix(entry: Dict[str, Any]) -> str:
- reason = (entry.get("reason") or "").strip()
- return f" — {reason}" if reason else ""
+ return f" — {reason}" if (reason := (entry.get("reason") or "").strip()) else ""
def _consolidated_lines(entry: Dict[str, Any]) -> List[str]:
@@ -830,14 +738,9 @@ def _consolidated_lines(entry: Dict[str, Any]) -> List[str]:
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
+ return [line] + ([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."]
+ if entry.get("model_claimed_into") else [])
def _pruned_lines(entry: Any) -> List[str]:
@@ -847,38 +750,28 @@ def _pruned_lines(entry: Any) -> List[str]:
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 [])]
- )
+ head = f"- `{job_name}`: `{', '.join(entry.get('before') or [])}` → `{', '.join(entry.get('after') or []) or '(none)'}`"
+ return ([head] + [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`"),
+ "_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",
+ "_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, ""),
+ ("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`"),
+ "_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`"),
)
@@ -886,66 +779,39 @@ def _render_report_markdown(p: Dict[str, Any]) -> str:
"""Render the human-readable REPORT.md."""
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 {}
+ counts, auto, tc_counts = p.get("counts") or {}, p.get("auto_transitions") or {}, p.get("tool_call_counts") or {}
error = p.get("llm_error")
-
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} · "
- f"Agent-created skills: {counts.get('before', 0)} → {counts.get('after', 0)} "
- f"({counts.get('delta', 0):+d})\n",
- ]
- if error:
- lines.append(f"> ⚠ LLM pass error: `{error}`\n")
- lines += [
- "## Auto-transitions (pure, no LLM)\n",
- f"- checked: {auto.get('checked', 0)}",
- f"- marked stale: {auto.get('marked_stale', 0)}",
- f"- archived (no LLM, pure time-based staleness): {auto.get('archived', 0)}",
- f"- reactivated: {auto.get('reactivated', 0)}",
- "",
+ f"Model: `{p.get('model') or '(not resolved)'}` via `{p.get('provider') or '(not resolved)'}` · Duration: {dur_label} · "
+ f"Agent-created skills: {counts.get('before', 0)} → {counts.get('after', 0)} ({counts.get('delta', 0):+d})\n",
+ *([f"> ⚠ LLM pass error: `{error}`\n"] if error else []),
+ "## Auto-transitions (pure, no LLM)\n", f"- checked: {auto.get('checked', 0)}", f"- marked stale: {auto.get('marked_stale', 0)}",
+ f"- archived (no LLM, pure time-based staleness): {auto.get('archived', 0)}", f"- reactivated: {auto.get('reactivated', 0)}", "",
"## LLM consolidation pass\n",
- f"- tool calls: **{counts.get('tool_calls_total', 0)}** "
- f"(by name: {', '.join(f'{k}={v}' for k, v in sorted(tc_counts.items())) or 'none'})",
+ f"- tool calls: **{counts.get('tool_calls_total', 0)}** (by name: {', '.join(f'{k}={v}' for k, v in sorted(tc_counts.items())) or 'none'})",
f"- consolidated into umbrellas: **{counts.get('consolidated_this_run', 0)}**",
- f"- pruned (archived for staleness): **{counts.get('pruned_this_run', 0)}**",
- f"- new skills this run: **{counts.get('added_this_run', 0)}**",
- f"- state transitions (active ↔ stale ↔ archived): "
- f"**{counts.get('state_transitions', 0)}**",
- "",
+ f"- pruned (archived for staleness): **{counts.get('pruned_this_run', 0)}**", f"- new skills this run: **{counts.get('added_this_run', 0)}**",
+ f"- state transitions (active ↔ stale ↔ archived): **{counts.get('state_transitions', 0)}**", "",
]
-
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)
+ lines += [f"### {title} ({len(items)})\n"] + ([intro] if intro else [])
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("")
-
+ lines += ([f"- … and {len(items) - show} more ({hint})"] if show is not None and len(items) > show else []) + [""]
final = (p.get("llm_final") or "").strip()
if final:
lines += ["## LLM final summary\n", final, ""]
elif not error and (p.get("llm_summary") or ""):
lines += ["## LLM summary\n", p.get("llm_summary"), ""]
-
- lines += [
- "## Recovery\n",
- "- Restore an archived skill: `hermes curator restore <name>`",
- "- All archives live under `~/.hermes/skills/.archive/` and are recoverable by `mv`",
- "- See `run.json` in this directory for the full machine-readable record.",
- "",
- ]
+ lines += ["## Recovery\n", "- Restore an archived skill: `hermes curator restore <name>`",
+ "- All archives live under `~/.hermes/skills/.archive/` and are recoverable by `mv`",
+ "- See `run.json` in this directory for the full machine-readable record.", ""]
return "\n".join(lines)
@@ -957,14 +823,13 @@ def _render_candidate_list() -> str:
if not rows:
return "No curator-managed skills to review."
cron_referenced = _cron_referenced_skills()
- lines = [f"Curator-managed skills ({len(rows)}):\n"] + [
+ return "\n".join([f"Curator-managed skills ({len(rows)}):\n"] + [
f"- {r['name']} provenance={r.get('provenance', 'agent')} state={r['state']} "
f"pinned={'yes' if r.get('pinned') else 'no'} cron={'yes' if r['name'] in cron_referenced else 'no'} "
f"activity={r.get('activity_count', 0)} use={r.get('use_count', 0)} view={r.get('view_count', 0)} "
f"patches={r.get('patch_count', 0)} last_activity={r.get('last_activity_at') or 'never'}"
for r in rows
- ]
- return "\n".join(lines)
+ ])
def _llm_meta(summary: str, error: Optional[str] = None) -> Dict[str, Any]:
@@ -974,36 +839,27 @@ def _llm_meta(summary: str, error: Optional[str] = None) -> Dict[str, Any]:
def _notify(on_summary: Optional[Callable[[str], None]], message: str) -> None:
if on_summary:
- try:
+ with contextlib.suppress(Exception):
on_summary(message)
- except Exception:
- pass
def _safe_curated_report() -> List[Dict[str, Any]]:
- try:
+ with contextlib.suppress(Exception):
return skill_usage.curated_report()
- except Exception:
- return []
-
-
-_AUTO_LABELS = (("marked_stale", "marked stale"), ("archived", "archived"), ("reactivated", "reactivated"))
+ 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."""
+ """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}"
+ # With prune-builtins on, bundled skills are candidates too: relax hard rule #1 for them (archive only; hub stays off-limits).
+ prompt = f"{CURATOR_REVIEW_PROMPT}{CURATOR_PRUNE_BUILTINS_NOTE if get_prune_builtins() else ''}\n\n{candidate_list}"
if dry_run:
prompt = f"{CURATOR_DRY_RUN_BANNER}\n\n{prompt}"
llm_meta = _run_llm_review(prompt)
@@ -1012,13 +868,10 @@ def _consolidation_pass(prefix: str, auto_summary: str, dry_run: bool, before_na
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 "",
+ 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}"
@@ -1031,19 +884,14 @@ def run_curator_review(
on_summary: Optional[Callable[[str], None]] = None, synchronous: bool = False,
dry_run: bool = False, consolidate: Optional[bool] = None,
) -> Dict[str, Any]:
- """Execute a single curator review pass: (1) automatic state transitions (no
- LLM); (2) if *consolidate* and there are candidates, fork an AIAgent on the
- review prompt; (3) update .curator_state; (4) call *on_summary*.
-
- *synchronous* runs the LLM review in the calling thread (default: daemon
- thread). *consolidate* ``None`` reads ``curator.consolidate`` (OFF by
- default); when off only the deterministic prune runs — no fork, no aux cost.
- *dry_run* SKIPS the stale/archive transitions and instructs the fork to
- report only; REPORT.md is still written and recorded in
- ``state.last_report_path`` so users can read what WOULD have happened.
- """
- if consolidate is None:
- consolidate = get_consolidate()
+ """Execute a single curator review pass: (1) automatic state transitions (no LLM); (2) if *consolidate* and there are
+ candidates, fork an AIAgent on the review prompt; (3) update .curator_state; (4) call *on_summary*.
+ *synchronous* runs the LLM review in the calling thread (default: daemon thread).
+ *consolidate* ``None`` reads ``curator.consolidate`` (OFF by default); when off
+ only the deterministic prune runs — no fork, no aux cost. *dry_run* SKIPS the
+ stale/archive transitions and instructs the fork to report only; REPORT.md is still
+ written and recorded in ``state.last_report_path`` so users can read what WOULD have happened."""
+ consolidate = get_consolidate() if consolidate is None else consolidate
start = datetime.now(timezone.utc)
if dry_run: # count candidates without mutating state
counts = {"checked": len(_safe_curated_report()), "marked_stale": 0, "archived": 0, "reactivated": 0}
@@ -1058,39 +906,31 @@ def run_curator_review(
except Exception as e:
logger.debug("Curator pre-run snapshot failed: %s", e, exc_info=True)
counts = apply_automatic_transitions(now=start)
-
auto_summary = ", ".join(
- f"{counts[key]} {label}" for key, label in _AUTO_LABELS if counts[key]
+ f"{counts[key]} {label}" for key, label in (("marked_stale", "marked stale"), ("archived", "archived"), ("reactivated", "reactivated")) if counts[key]
) or "no changes"
# Persist before the LLM pass so a crash mid-review still records the run.
# Dry-run does NOT bump last_run_at/run_count (a preview must not push the
# next real pass out) but still records a summary for `hermes curator status`.
- state = load_state()
- if not dry_run:
- state["last_run_at"] = start.isoformat()
- state["run_count"] = int(state.get("run_count", 0)) + 1
prefix = "dry-run auto: " if dry_run else "auto: "
- state["last_run_summary"] = f"{prefix}{auto_summary}"
+ state = {**load_state(), "last_run_summary": f"{prefix}{auto_summary}"}
+ if not dry_run:
+ state.update(last_run_at=start.isoformat(), run_count=int(state.get("run_count", 0)) + 1)
save_state(state)
def _llm_pass():
# Snapshot skill state BEFORE the LLM pass so the report can diff.
before_report = _safe_curated_report()
before_names = set(_by_name(before_report))
-
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)")
-
elapsed = (datetime.now(timezone.utc) - start).total_seconds()
- state2 = load_state()
- state2["last_run_duration_seconds"] = elapsed
- state2["last_run_summary"] = final_summary
-
+ state2 = {**load_state(), "last_run_duration_seconds": elapsed, "last_run_summary": final_summary}
# Per-run report, best-effort; path recorded for `hermes curator status`.
try:
report_path = _write_run_report(
@@ -1108,7 +948,6 @@ def run_curator_review(
_llm_pass()
else:
threading.Thread(target=_llm_pass, daemon=True, name="curator-review").start()
-
return {"started_at": start.isoformat(), "auto_transitions": counts, "summary_so_far": auto_summary}
@@ -1116,7 +955,6 @@ def run_curator_review(
class _ReviewRuntimeBinding(NamedTuple):
"""Provider/model for the curator review fork plus per-slot overrides."""
-
provider: str
model: str
explicit_api_key: Optional[str]
@@ -1124,10 +962,6 @@ class _ReviewRuntimeBinding(NamedTuple):
request_overrides: Dict[str, Any]
-def _strip_aux_credential(value: Any) -> Optional[str]:
- return (str(value).strip() or None) if value is not None else None
-
-
def _merge_request_overrides(runtime_overrides: Any, slot_extra_body: Any) -> Dict[str, Any]:
"""Merge resolver metadata with task-local request body fields."""
merged = dict(runtime_overrides or {})
@@ -1137,47 +971,35 @@ def _merge_request_overrides(runtime_overrides: Any, slot_extra_body: Any) -> Di
def _slot_binding(provider: str, model: str, slot: Dict[str, Any]) -> _ReviewRuntimeBinding:
- return _ReviewRuntimeBinding(
- provider, model, _strip_aux_credential(slot.get("api_key")),
- _strip_aux_credential(slot.get("base_url")), _merge_request_overrides({}, slot.get("extra_body")),
- )
+ api_key, base_url = ((str(v).strip() or None) if v is not None else None for v in (slot.get("api_key"), slot.get("base_url")))
+ return _ReviewRuntimeBinding(provider, model, api_key, base_url, _merge_request_overrides({}, slot.get("extra_body")))
def _resolve_review_runtime(cfg: Dict[str, Any]) -> _ReviewRuntimeBinding:
- """Curator is a regular auxiliary task slot (``auxiliary.curator.*``), so it
- rides the canonical aux-model plumbing. Precedence:
+ """Curator is a regular auxiliary task slot (``auxiliary.curator.*``), so it rides the canonical aux-model plumbing. Precedence:
1. ``auxiliary.curator.{provider,model}`` when both are set non-auto
2. Legacy ``curator.auxiliary.{provider,model}`` (deprecated) when both set
3. Main ``model.{provider,default/model}`` pair ("auto" + "" = main chat model)
- Non-empty slot ``api_key``/``base_url`` are returned as explicit overrides so
- ``resolve_runtime_provider`` doesn't reuse the main chat credential chain.
- """
+ Non-empty slot ``api_key``/``base_url`` are returned as explicit overrides so ``resolve_runtime_provider`` doesn't reuse the main chat credential chain."""
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}"
- )
+ 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)
-
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."""
+ """``(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, ""
+ overrides, provider, model_name = {}, None, ""
try:
from hermes_cli.config import load_config_readonly
from hermes_cli.runtime_provider import resolve_runtime_provider
@@ -1197,46 +1019,32 @@ def _resolve_review_provider() -> tuple:
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."""
+ """[{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 {}
+ for fn in ((tc.get("function") or {}) for tc in (msg.get("tool_calls") or []) if isinstance(tc, dict)) if isinstance(msg, dict) else []:
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})
+ calls.append({"name": fn.get("name") or "", "arguments": args_raw[:400] + "…" if isinstance(args_raw, str) and len(args_raw) > 400 else args_raw})
return calls
def _run_llm_review(prompt: str) -> Dict[str, Any]:
- """Spawn an AIAgent fork on the review prompt. Returns ``final`` (untruncated
- response), ``summary`` (240-char cap), ``model``/``provider`` (what ran),
- ``tool_calls`` ([{name, arguments}], truncated) and ``error``. Never raises."""
- import contextlib
+ """Spawn an AIAgent fork on the review prompt. Returns ``final`` (untruncated response), ``summary`` (240-char cap),
+ ``model``/``provider`` (what ran), ``tool_calls`` ([{name, arguments}], truncated) and ``error``. Never raises."""
result_meta: Dict[str, Any] = _llm_meta("")
try:
from run_agent import AIAgent
except Exception as e:
result_meta["error"] = result_meta["summary"] = f"AIAgent import failed: {e}"
return result_meta
-
rp, model_name, provider, request_overrides = _resolve_review_provider()
- result_meta["model"] = model_name
- result_meta["provider"] = provider or ""
-
+ result_meta["model"], result_meta["provider"] = model_name, provider or ""
review_agent = None
try:
- agent_kwargs: Dict[str, Any] = {}
- if isinstance(rp.get("max_output_tokens"), int):
- agent_kwargs["max_tokens"] = rp["max_output_tokens"]
+ agent_kwargs: Dict[str, Any] = {"max_tokens": rp["max_output_tokens"]} if isinstance(rp.get("max_output_tokens"), int) else {}
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.update(acp_command=acp_command, acp_args=list(rp.get("args") or []))
review_agent = AIAgent(
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"),
@@ -1257,12 +1065,10 @@ def _run_llm_review(prompt: str) -> Dict[str, Any]:
# write guards (external/bundled/hub) fire; turn_context binds this onto
# the write-origin ContextVar at turn start.
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):
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"] = final
result_meta["summary"] = (final[:240] + "…") if len(final) > 240 else (final or "no change")
@@ -1271,20 +1077,15 @@ def _run_llm_review(prompt: str) -> Dict[str, Any]:
result_meta["error"] = result_meta["summary"] = f"error: {e}"
finally:
if review_agent is not None:
- try:
+ with contextlib.suppress(Exception):
review_agent.close()
- except Exception:
- pass
return result_meta
# --- Public entrypoint for the session-start hook ---
-def maybe_run_curator(
- *, idle_for_seconds: Optional[float] = None, on_summary: Optional[Callable[[str], None]] = None,
-) -> Optional[Dict[str, Any]]:
- """Best-effort: run a curator pass if all gates pass. Returns the result
- dict if a pass was started, else None. Never raises."""
+def maybe_run_curator(*, idle_for_seconds: Optional[float] = None, on_summary: Optional[Callable[[str], None]] = None) -> Optional[Dict[str, Any]]:
+ """Best-effort: run a curator pass if all gates pass. Returns the result dict if a pass was started, else None. Never raises."""
try:
# Idle gating: only enforce when the caller provided a measurement.
if not should_run_now() or (idle_for_seconds is not None and idle_for_seconds < get_min_idle_hours() * 3600.0):
diff --git a/agent/insights.py b/agent/insights.py
index 4819c24af90..0880ae9de65 100644
--- a/agent/insights.py
+++ b/agent/insights.py
@@ -469,8 +469,14 @@ class InsightsEngine:
period_label += f" ({report['source_filter']})"
padding = 58 - len(period_label) - 2
left_pad = padding // 2
- lines = ["", " ╔══════════════════════════════════════════════════════════╗", " ║ 📊 Hermes Insights ║",
- f" ║{' ' * left_pad} {period_label} {' ' * (padding - left_pad)}║", " ╚══════════════════════════════════════════════════════════╝", ""]
+ lines = [
+ "",
+ " ╔══════════════════════════════════════════════════════════╗",
+ " ║ 📊 Hermes Insights ║",
+ f" ║{' ' * left_pad} {period_label} {' ' * (padding - left_pad)}║",
+ " ╚══════════════════════════════════════════════════════════╝",
+ "",
+ ]
if o.get("date_range_start") and o.get("date_range_end"):
start_str = datetime.fromtimestamp(o["date_range_start"]).strftime("%b %d, %Y")
end_str = datetime.fromtimestamp(o["date_range_end"]).strftime("%b %d, %Y")