Merge branch 'simp/r3-07-E2' into simp/integration3

This commit is contained in:
Teknium
2026-09-02 21:58:55 -07:00
6 changed files with 169 additions and 358 deletions
+37 -62
View File
@@ -18,6 +18,16 @@ from agent.turn_context import drop_stale_api_content
logger = logging.getLogger(__name__)
# Orphan-recovery notices: (side-effecting, read-only) for an interrupted block vs a dangling tail.
_INTERRUPTED_NOTICES = (
"[Orphan recovery: interrupted side-effecting tool may have executed; its effect is UNKNOWN. Inspect state before retrying.]",
"[Orphan recovery: interrupted read-only tool did not complete.]",
)
_DANGLING_NOTICES = (
"[Orphan recovery: this tool may have executed before Hermes stopped; its effect is UNKNOWN. Inspect current state before retrying.]",
"[Orphan recovery: this read-only tool did not complete and had no effect.]",
)
def is_interrupted_tool_result(content: Any) -> bool:
"""Return True if a tool result indicates the tool was interrupted."""
@@ -26,9 +36,7 @@ def is_interrupted_tool_result(content: Any) -> bool:
lowered = content.lower()
if "[command interrupted]" in lowered:
return True
if "exit_code" in lowered and ("130" in lowered or "-1" in lowered):
return "interrupt" in lowered
return False
return "exit_code" in lowered and ("130" in lowered or "-1" in lowered) and "interrupt" in lowered
def _call_name(call: Dict[str, Any]) -> str:
@@ -43,11 +51,11 @@ def _any_side_effecting(calls: List[Dict[str, Any]]) -> bool:
return any(tool_may_have_side_effect(_call_name(call)) for call in calls)
def _orphan_recovery(name: str, unknown_text: str, none_text: str) -> tuple:
def _orphan_recovery(name: str, notices: tuple) -> tuple:
"""(effect_disposition, content) for an interrupted/dangling call named ``name``."""
if tool_may_have_side_effect(name):
return "unknown", unknown_text
return "none", none_text
return "unknown", notices[0]
return "none", notices[1]
def strip_interrupted_tool_tails(agent_history: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
@@ -56,7 +64,6 @@ def strip_interrupted_tool_tails(agent_history: List[Dict[str, Any]]) -> List[Di
interrupted results rewritten as orphan-recovery notices, since the effect may have happened."""
if not agent_history:
return agent_history
cleaned: List[Dict[str, Any]] = []
i = 0
n = len(agent_history)
@@ -64,10 +71,9 @@ def strip_interrupted_tool_tails(agent_history: List[Dict[str, Any]]) -> List[Di
msg = agent_history[i]
if msg.get("role") == "assistant" and "tool_calls" in msg:
j = i + 1
tool_results: List[Dict[str, Any]] = []
while j < n and agent_history[j].get("role") == "tool":
tool_results.append(agent_history[j])
j += 1
tool_results = agent_history[i + 1:j]
if tool_results and any(is_interrupted_tool_result(m.get("content", "")) for m in tool_results):
calls = msg.get("tool_calls") or []
if _any_side_effecting(calls):
@@ -79,28 +85,20 @@ def strip_interrupted_tool_tails(agent_history: List[Dict[str, Any]]) -> List[Di
continue
recovered = dict(tool_result)
name = call_names.get(str(tool_result.get("tool_call_id") or ""), "")
recovered["effect_disposition"], recovered["content"] = _orphan_recovery(
name,
"[Orphan recovery: interrupted side-effecting tool may have "
"executed; its effect is UNKNOWN. Inspect state before retrying.]",
"[Orphan recovery: interrupted read-only tool did not complete.]",
)
recovered["effect_disposition"], recovered["content"] = _orphan_recovery(name, _INTERRUPTED_NOTICES)
cleaned.append(recovered)
else:
logger.debug(
"Stripping interrupted read-only assistant→tool replay block "
"(indices %d–%d, tool_results=%d)",
"Stripping interrupted read-only assistant→tool replay block (indices %d–%d, tool_results=%d)",
i, j - 1, len(tool_results),
)
i = j
continue
if msg.get("role") == "tool" and is_interrupted_tool_result(msg.get("content", "")):
logger.debug("Stripping orphan interrupted tool result from replay history")
i += 1
continue
cleaned.append(msg)
else:
cleaned.append(msg)
i += 1
return cleaned
@@ -111,26 +109,18 @@ def strip_dangling_tool_call_tail(agent_history: List[Dict[str, Any]]) -> List[D
ones get synthetic UNKNOWN-effect results."""
if not agent_history:
return agent_history
last = agent_history[-1]
if not (isinstance(last, dict) and last.get("role") == "assistant" and last.get("tool_calls")):
return agent_history
tool_calls = last.get("tool_calls") or []
if _any_side_effecting(tool_calls):
recovered = list(agent_history)
for call in tool_calls:
name = str((call.get("function") or {}).get("name") or "unknown")
disposition, content = _orphan_recovery(
name,
"[Orphan recovery: this tool may have executed before Hermes stopped; "
"its effect is UNKNOWN. Inspect current state before retrying.]",
"[Orphan recovery: this read-only tool did not complete and had no effect.]",
)
name = _call_name(call) or "unknown"
disposition, content = _orphan_recovery(name, _DANGLING_NOTICES)
recovered.append(make_tool_result_message(name, content, _call_id(call), effect_disposition=disposition))
logger.warning("Recovered dangling side-effecting tool call(s) as UNKNOWN instead of erasing them")
return recovered
logger.debug("Stripping dangling unanswered read-only assistant(tool_calls) tail (%d call(s))", len(tool_calls))
return agent_history[:-1]
@@ -149,20 +139,11 @@ def sanitize_replay_history(agent_history: List[Dict[str, Any]]) -> List[Dict[st
_DANGEROUS_CONFIRMATION_EXPIRY_SECONDS = 60.0
# Confirmation phrases that unlock destructive host actions; case-insensitive substring match so
# trailing punctuation / extra context still matches.
# trailing punctuation / extra context still matches. Includes i18n variants from the original incident.
_DANGEROUS_CONFIRMATION_PATTERNS: tuple = (
"confirm forced restart",
"confirm forced reboot",
"confirm shutdown",
"confirm reboot",
"confirm power off",
"yes, delete everything",
"confirm wipe",
"confirm factory reset",
# i18n variants observed in the original incident
"確認強制重開機",
"確認強制重開",
"確認重啟",
"confirm forced restart", "confirm forced reboot", "confirm shutdown", "confirm reboot", "confirm power off",
"yes, delete everything", "confirm wipe", "confirm factory reset",
"確認強制重開機", "確認強制重開", "確認重啟",
)
# Redacting in place (rather than deleting the message) preserves strict user/assistant role
@@ -193,26 +174,20 @@ def strip_stale_dangerous_confirmations(
without a timestamp (legacy transcripts, test scaffolding) are left untouched."""
if not agent_history:
return agent_history
cleaned: List[Dict[str, Any]] = []
for msg in agent_history:
ts = msg.get("timestamp") if isinstance(msg, dict) and msg.get("role") == "user" else None
if (
ts is not None
and is_dangerous_confirmation(msg.get("content", ""))
and (now - float(ts)) > expiry_seconds
):
logger.debug(
"Redacting stale dangerous-confirmation text in user "
"message (age=%.1fs, expiry=%.1fs): %r",
now - float(ts), expiry_seconds, (msg.get("content") or "")[:80],
)
redacted = dict(msg)
redacted["content"] = _EXPIRED_CONFIRMATION_SENTINEL
# The api_content sidecar carries the exact bytes previously sent — the confirmation
# itself; replaying it would undo the redaction.
drop_stale_api_content(redacted)
cleaned.append(redacted)
if ts is None or not is_dangerous_confirmation(msg.get("content", "")) or (now - float(ts)) <= expiry_seconds:
cleaned.append(msg)
continue
cleaned.append(msg)
logger.debug(
"Redacting stale dangerous-confirmation text in user message (age=%.1fs, expiry=%.1fs): %r",
now - float(ts), expiry_seconds, (msg.get("content") or "")[:80],
)
redacted = dict(msg)
redacted["content"] = _EXPIRED_CONFIRMATION_SENTINEL
# The api_content sidecar carries the exact bytes previously sent — the confirmation
# itself; replaying it would undo the redaction.
drop_stale_api_content(redacted)
cleaned.append(redacted)
return cleaned
+44 -55
View File
@@ -62,12 +62,10 @@ def _safe_session_filename_component(session_id: str) -> str:
"""Path-safe filename component for a (possibly untrusted ``X-Hermes-Session-Id``) session ID:
non ``[A-Za-z0-9_-]`` → ``_``, capped, plus a content hash when changed so distinct IDs cannot collide."""
raw = str(session_id or "").strip()
sanitized = re.sub(r"[^\w-]", "_", raw).strip("._")
sanitized = sanitized[:96] or "session"
sanitized = re.sub(r"[^\w-]", "_", raw).strip("._")[:96] or "session"
if raw and sanitized == raw:
return sanitized
digest = hashlib.sha256(raw.encode("utf-8", errors="surrogatepass")).hexdigest()[:12]
return f"{sanitized}_{digest}"
return f"{sanitized}_{hashlib.sha256(raw.encode('utf-8', errors='surrogatepass')).hexdigest()[:12]}"
def _override_replaces_content(msg: Dict, content: Any, override: Any) -> bool:
@@ -100,17 +98,14 @@ def _durable_content(content: Any) -> Any:
"""Text-only DB projection: multimodal envelopes → summary; part lists keep text, images → ``[screenshot]``."""
if _is_multimodal_tool_result(content):
return _multimodal_text_summary(content)
if isinstance(content, list):
txt = []
for p in content:
if not isinstance(p, dict):
continue
if p.get("type") == "text":
txt.append(str(p.get("text", "")))
elif p.get("type") in _IMAGE_PART_TYPES:
txt.append("[screenshot]")
return "\n".join(txt) if txt else None
return content
if not isinstance(content, list):
return content
txt = [
str(p.get("text", "")) if p.get("type") == "text" else "[screenshot]"
for p in content
if isinstance(p, dict) and (p.get("type") == "text" or p.get("type") in _IMAGE_PART_TYPES)
]
return "\n".join(txt) if txt else None
def _tool_calls_data(msg: Dict) -> Any:
@@ -121,6 +116,11 @@ def _tool_calls_data(msg: Dict) -> Any:
return None
def _persist_lock(agent):
"""Close and turn-start persistence can run on separate CLI threads: one critical section."""
return getattr(agent, "_session_persist_lock", None) or nullcontext()
# --- flush phases (module-level so the flush also works bound onto duck-typed agents) ---
@@ -138,12 +138,10 @@ def _db_flush_seed_ids(agent) -> set:
def _db_flush_scan_start(agent, messages: List[Dict]) -> int:
"""Skip the identity-matched, still-marked prefix of the previous flush's snapshot."""
scan_start = 0
prev_prefix = getattr(agent, "_db_flush_scan_prefix", None)
if isinstance(prev_prefix, list):
for prev, cur in zip(prev_prefix, messages):
if cur is not prev or not cur.get(_DB_PERSISTED_MARKER):
break
scan_start += 1
for prev, cur in zip(getattr(agent, "_db_flush_scan_prefix", None) or (), messages):
if cur is not prev or not cur.get(_DB_PERSISTED_MARKER):
break
scan_start += 1
return scan_start
@@ -209,11 +207,9 @@ def _db_flush_collect(agent, messages: List[Dict], conversation_history: Optiona
batch_msgs: List[Dict] = []
for msg_idx in range(_db_flush_scan_start(agent, messages), len(messages)):
msg = messages[msg_idx]
if not isinstance(msg, dict):
continue
# The flush is append-only: a mid-turn persist of scaffolding could commit a synthetic
# turn the end-of-turn drop cannot un-write. Skip regardless of position.
if _is_ephemeral_scaffolding(msg) or msg.get(_DB_PERSISTED_MARKER):
if not isinstance(msg, dict) or _is_ephemeral_scaffolding(msg) or msg.get(_DB_PERSISTED_MARKER):
continue
# Already durable (history copy or caller-seeded): stamp so future flushes skip it.
if id(msg) in history_ids or id(msg) in seed_ids:
@@ -309,6 +305,22 @@ def _session_log_entry(agent, msg: Dict[str, Any]) -> Dict[str, Any]:
return {**msg, "content": agent._redact_message_content(content)}
def _existing_log_is_larger(log_file, count: int) -> bool:
"""Never overwrite a larger log with fewer messages (resumed agent with partial history);
a corrupted existing file allows the overwrite."""
if not log_file.exists():
return False
try:
existing = json.loads(log_file.read_text(encoding="utf-8"))
existing_count = existing.get("message_count", len(existing.get("messages", [])))
except Exception:
return False
if existing_count > count:
logging.debug("Skipping session log overwrite: existing has %d messages, current has %d", existing_count, count)
return True
return False
class SessionPersistenceMixin:
"""Session DB flush, session log and trajectory persistence (see module docstring)."""
@@ -335,10 +347,9 @@ class SessionPersistenceMixin:
def _persist_session(self, messages: List[Dict], conversation_history: List[Dict] = None):
"""Save session state to both JSON log and SQLite on any exit path. Trailing empty-response
scaffolding is dropped from the live list; the persist override is applied to the DB row only."""
# Close and turn-start persistence can run on separate CLI threads: one critical section.
from agent.agent_runtime_helpers import note_turn_persisted
with getattr(self, "_session_persist_lock", None) or nullcontext():
with _persist_lock(self):
self._drop_trailing_empty_response_scaffolding(messages)
self._session_messages = messages
self._save_session_log(messages)
@@ -374,14 +385,11 @@ class SessionPersistenceMixin:
def _flush_messages_to_session_db(self, messages: List[Dict], conversation_history: Optional[List[Dict]] = None):
"""Serialize direct and turn-boundary session flushes per agent."""
with getattr(self, "_session_persist_lock", None) or nullcontext():
with _persist_lock(self):
return self._flush_messages_to_session_db_unlocked(messages, conversation_history)
def _flush_messages_to_session_db_unlocked(
self,
messages: List[Dict],
conversation_history: Optional[List[Dict]] = None,
_adoption_budget: int = 1,
self, messages: List[Dict], conversation_history: Optional[List[Dict]] = None, _adoption_budget: int = 1,
):
"""Persist un-flushed messages to SQLite. Dedup is the intrinsic ``_DB_PERSISTED_MARKER`` on
each written dict — not positional slices (drift after sequence repair) nor an ``id(msg)`` set
@@ -389,9 +397,7 @@ class SessionPersistenceMixin:
session adopts its live tip and retries exactly once."""
# Persistence-isolated agents (background review fork) share the parent's session_id for cache
# warmth; a write here would land the curator's turn in the user's real history.
if getattr(self, "_persist_disabled", False):
return None
if not self._session_db:
if getattr(self, "_persist_disabled", False) or not self._session_db:
return None
batch_rows: List[Dict[str, Any]] = []
try:
@@ -420,7 +426,6 @@ class SessionPersistenceMixin:
return messages.copy()
_format_tools_for_system_message = _forward("agent.system_prompt", "format_tools_for_system_message")
_convert_to_trajectory_format = _forward("agent.agent_runtime_helpers", "convert_to_trajectory_format")
def _save_trajectory(self, messages: List[Dict[str, Any]], user_query: str, completed: bool):
@@ -431,7 +436,6 @@ class SessionPersistenceMixin:
_save_trajectory_to_file(trajectory, self.model, completed)
_extract_api_error_context = _forward_static("agent.agent_runtime_helpers", "extract_api_error_context")
_dump_api_request_debug = _forward("agent.agent_runtime_helpers", "dump_api_request_debug")
@staticmethod
@@ -459,8 +463,7 @@ class SessionPersistenceMixin:
def _save_session_log(self, messages: List[Dict[str, Any]] = None):
"""Optional per-session JSON snapshot (``sessions.write_json_snapshots``, default False) for
external tooling; state.db is canonical. Rewrites the full list after every persistence point,
never overwriting a larger log with fewer messages (resumed agent with partial history)."""
external tooling; state.db is canonical. Rewrites the full list after every persistence point."""
if not getattr(self, "_session_json_enabled", False):
return
messages = messages or self._session_messages
@@ -468,28 +471,14 @@ class SessionPersistenceMixin:
return
# Re-derive the path each call so /branch and /compress land in the right file.
try:
safe_sid = _safe_session_filename_component(self.session_id)
log_file = self.logs_dir / f"session_{safe_sid}.json"
log_file = self.logs_dir / f"session_{_safe_session_filename_component(self.session_id)}.json"
except Exception:
return
try:
# Mirror the SQLite flush: scaffolding is never durable transcript content.
cleaned = [_session_log_entry(self, msg) for msg in messages if not _is_ephemeral_scaffolding(msg)]
if log_file.exists():
try:
existing = json.loads(log_file.read_text(encoding="utf-8"))
existing_count = existing.get("message_count", len(existing.get("messages", [])))
if existing_count > len(cleaned):
logging.debug(
"Skipping session log overwrite: existing has %d messages, current has %d",
existing_count, len(cleaned),
)
return
except Exception:
pass # corrupted existing file — allow the overwrite
if _existing_log_is_larger(log_file, len(cleaned)):
return
entry = {
"session_id": self.session_id,
"model": self.model,
+39 -90
View File
@@ -26,7 +26,8 @@ from datetime import datetime, timezone
from pathlib import Path
from typing import Any, Callable, Dict, Iterator, List, Optional, Set, Tuple
from hermes_cli._subprocess_compat import IS_WINDOWS, kill_process_tree, windows_hide_flags
# split_command_line, not shlex: shlex eats Windows path backslashes.
from hermes_cli._subprocess_compat import IS_WINDOWS, kill_process_tree, split_command_line, windows_hide_flags
try:
import fcntl # POSIX only; Windows falls back to best-effort without flock.
@@ -73,12 +74,6 @@ def _forget_home_registrations(registry: Set[tuple], lock: threading.Lock) -> No
registry.difference_update({k for k in registry if k[0] == home_key})
def _split(command: str) -> List[str]:
from hermes_cli._subprocess_compat import split_command_line # Windows-safe: shlex eats path backslashes
return split_command_line(command)
def _entry_matches(e: Any, event: Optional[str], command: str) -> bool:
return isinstance(e, dict) and (event is None or e.get("event") == event) and e.get("command") == command
@@ -151,25 +146,21 @@ def register_from_config(cfg: Optional[Dict[str, Any]], *, accept_hooks: bool =
newly wired up; skipped entries (unknown, malformed, not allowlisted, registered) are logged only."""
if not isinstance(cfg, dict):
return []
# Safe mode: hooks are user customizations too — fire zero user-configured code.
from utils import env_var_enabled
if env_var_enabled("HERMES_SAFE_MODE"):
logger.info("HERMES_SAFE_MODE=1 — shell-hook registration skipped")
return []
effective_accept = _resolve_effective_accept(cfg, accept_hooks)
specs = _parse_hooks_block(cfg.get("hooks"))
if not specs:
return []
registered: List[ShellHookSpec] = []
from hermes_cli.plugins import get_plugin_manager # lazy: avoids import cycle
manager = get_plugin_manager()
home_key = _home_key()
# Idempotence + allowlist read happen under the lock; the TTY prompt runs outside it; mutation
# re-takes the lock and re-checks in case two callers raced.
for spec in specs:
@@ -178,7 +169,6 @@ def register_from_config(cfg: Optional[Dict[str, Any]], *, accept_hooks: bool =
if key in _registered:
continue
already_allowlisted = _is_allowlisted(spec.event, spec.command)
if not already_allowlisted and not _prompt_and_record(spec.event, spec.command, accept_hooks=effective_accept):
logger.warning(
"shell hook for %s (%s) not allowlisted — skipped. Use --accept-hooks / "
@@ -186,7 +176,6 @@ def register_from_config(cfg: Optional[Dict[str, Any]], *, accept_hooks: bool =
spec.event, spec.command,
)
continue
with _registered_lock:
if key in _registered:
continue
@@ -197,15 +186,12 @@ def register_from_config(cfg: Optional[Dict[str, Any]], *, accept_hooks: bool =
"shell hook registered: %s -> %s (matcher=%s, timeout=%ds, fail_closed=%s)",
spec.event, spec.command, spec.matcher, spec.timeout, spec.fail_closed,
)
return registered
def iter_configured_hooks(cfg: Optional[Dict[str, Any]]) -> List[ShellHookSpec]:
"""Parse config hooks without registering (``hermes hooks list`` / doctor)."""
if not isinstance(cfg, dict):
return []
return _parse_hooks_block(cfg.get("hooks"))
return _parse_hooks_block(cfg.get("hooks")) if isinstance(cfg, dict) else []
def re_register_config_hooks() -> None:
@@ -231,11 +217,9 @@ def _parse_hooks_block(hooks_cfg: Any) -> List[ShellHookSpec]:
if not isinstance(hooks_cfg, dict):
return []
specs: List[ShellHookSpec] = []
for event_name, entries in hooks_cfg.items():
# Reserved non-event sub-sections nested under `hooks:`.
if event_name in ("output_spill", "outbound"):
if event_name in ("output_spill", "outbound"): # reserved non-event sub-sections under `hooks:`
continue
if event_name in SHELL_UNSUPPORTED_HOOKS:
# _parse_response has no channel for these directives — refuse loudly.
@@ -258,7 +242,6 @@ def _parse_hooks_block(hooks_cfg: Any) -> List[ShellHookSpec]:
logger.warning("hooks.%s must be a list of hook definitions; got %s", event_name, type(entries).__name__)
continue
specs.extend(filter(None, (_parse_single_entry(event_name, i, raw) for i, raw in enumerate(entries))))
return specs
@@ -269,12 +252,10 @@ def _parse_single_entry(event: str, index: int, raw: Any) -> Optional[ShellHookS
if not isinstance(raw, dict):
warn(" must be a mapping with a 'command' key; got %s", type(raw).__name__)
return None
command = raw.get("command")
if not isinstance(command, str) or not command.strip():
warn(" is missing a non-empty 'command' field")
return None
matcher = raw.get("matcher")
if matcher is not None and not isinstance(matcher, str):
warn(".matcher must be a string regex; ignoring")
@@ -286,12 +267,10 @@ def _parse_single_entry(event: str, index: int, raw: Any) -> Optional[ShellHookS
matcher, event,
)
matcher = None
timeout_raw = raw.get("timeout", DEFAULT_TIMEOUT_SECONDS)
try:
timeout = int(timeout_raw)
timeout = int(raw.get("timeout", DEFAULT_TIMEOUT_SECONDS))
except (TypeError, ValueError):
warn(".timeout must be an int (got %r); using default %ds", timeout_raw, DEFAULT_TIMEOUT_SECONDS)
warn(".timeout must be an int (got %r); using default %ds", raw.get("timeout"), DEFAULT_TIMEOUT_SECONDS)
timeout = DEFAULT_TIMEOUT_SECONDS
if timeout < 1:
warn(".timeout must be >=1; using default %ds", DEFAULT_TIMEOUT_SECONDS)
@@ -299,7 +278,6 @@ def _parse_single_entry(event: str, index: int, raw: Any) -> Optional[ShellHookS
if timeout > MAX_TIMEOUT_SECONDS:
warn(".timeout=%ds exceeds max %ds; clamping", timeout, MAX_TIMEOUT_SECONDS)
timeout = MAX_TIMEOUT_SECONDS
# ``fail_closed`` (canonical) wins over ``failClosed`` (Cursor/Claude-Code compat).
fail_closed = raw.get("fail_closed", raw.get("failClosed", False))
if not isinstance(fail_closed, bool):
@@ -312,18 +290,20 @@ def _parse_single_entry(event: str, index: int, raw: Any) -> Optional[ShellHookS
", ".join(sorted(_BLOCKING_EVENTS)), event,
)
fail_closed = False
return ShellHookSpec(event=event, command=command.strip(), matcher=matcher, timeout=timeout, fail_closed=fail_closed)
# --- Subprocess callback ---
# Popen failure -> diagnostic; anything else is reported as str(exc).
_POPEN_ERRORS = ((FileNotFoundError, "command not found"), (PermissionError, "command not executable"))
def _spawn(spec: ShellHookSpec, stdin_json: str) -> Dict[str, Any]:
"""Run ``spec.command`` with ``stdin_json`` on stdin; the single subprocess site.
Returns a diagnostic dict with the same keys for every outcome."""
result: Dict[str, Any] = {
"returncode": None, "stdout": "", "stderr": "",
"timed_out": False, "elapsed_seconds": 0.0, "error": None,
"returncode": None, "stdout": "", "stderr": "", "timed_out": False, "elapsed_seconds": 0.0, "error": None,
}
def failed(error: str) -> Dict[str, Any]:
@@ -331,12 +311,11 @@ def _spawn(spec: ShellHookSpec, stdin_json: str) -> Dict[str, Any]:
return result
try:
argv = _split(os.path.expanduser(spec.command))
argv = split_command_line(os.path.expanduser(spec.command))
except ValueError as exc:
return failed(f"command {spec.command!r} cannot be parsed: {exc}")
if not argv:
return failed("empty command")
t0 = time.monotonic()
# Own process group on POSIX so a timed-out hook's descendants are reaped with it; Windows tree
# cleanup goes through kill_process_tree (taskkill /T). Hooks that finish in time keep detached
@@ -344,18 +323,11 @@ def _spawn(spec: ShellHookSpec, stdin_json: str) -> Dict[str, Any]:
popen_kwargs: Dict[str, Any] = {"creationflags": windows_hide_flags()} if IS_WINDOWS else {"process_group": 0}
try:
proc = subprocess.Popen(
argv,
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
text=True, encoding='utf-8', errors='replace', shell=False,
**popen_kwargs,
argv, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
text=True, encoding='utf-8', errors='replace', shell=False, **popen_kwargs,
)
except FileNotFoundError:
return failed("command not found")
except PermissionError:
return failed("command not executable")
except Exception as exc: # pragma: no cover — defensive
return failed(str(exc))
except Exception as exc:
return failed(next((msg for cls, msg in _POPEN_ERRORS if isinstance(exc, cls)), str(exc)))
try:
stdout, stderr = proc.communicate(input=stdin_json, timeout=spec.timeout)
except Exception as exc:
@@ -368,10 +340,8 @@ def _spawn(spec: ShellHookSpec, stdin_json: str) -> Dict[str, Any]:
result["timed_out"] = True
result["elapsed_seconds"] = round(time.monotonic() - t0, 3)
return result
result.update(
returncode=proc.returncode, stdout=stdout or "", stderr=stderr or "",
elapsed_seconds=round(time.monotonic() - t0, 3),
returncode=proc.returncode, stdout=stdout or "", stderr=stderr or "", elapsed_seconds=round(time.monotonic() - t0, 3),
)
return result
@@ -384,8 +354,7 @@ def _make_callback(spec: ShellHookSpec) -> Callable[..., Optional[Dict[str, Any]
return None
return _evaluate_result(spec, _spawn(spec, _serialize_payload(spec.event, kwargs)))
_callback.__name__ = f"shell_hook[{spec.event}:{spec.command}]"
_callback.__qualname__ = _callback.__name__
_callback.__name__ = _callback.__qualname__ = f"shell_hook[{spec.event}:{spec.command}]"
return _callback
@@ -400,39 +369,30 @@ def _evaluate_result(spec: ShellHookSpec, r: Dict[str, Any]) -> Optional[Dict[st
stdout; unparseable stdout on a fail_closed hook blocks."""
blocking_event = spec.event in _BLOCKING_EVENTS
fail_closed = spec.fail_closed and blocking_event
if r["error"]:
logger.warning("shell hook failed (event=%s command=%s): %s", spec.event, spec.command, r["error"])
return _fail_closed_block(spec, r["error"]) if fail_closed else None
if r["timed_out"]:
logger.warning("shell hook timed out after %.2fs (event=%s command=%s)", r["elapsed_seconds"], spec.event, spec.command)
return _fail_closed_block(spec, f"timed out after {spec.timeout}s") if fail_closed else None
stderr = r["stderr"].strip()
if stderr:
logger.debug("shell hook stderr (event=%s command=%s): %s", spec.event, spec.command, stderr[:_STDERR_MESSAGE_LIMIT])
if r["returncode"] == BLOCK_EXIT_CODE and blocking_event:
parsed = _parse_response(spec.event, r["stdout"])
if isinstance(parsed, dict) and parsed.get("action") == "block":
return parsed
message = stderr[:_STDERR_MESSAGE_LIMIT] or _DEFAULT_BLOCK_MESSAGE
logger.info(
"shell hook exited %d — blocking (event=%s command=%s): %s",
BLOCK_EXIT_CODE, spec.event, spec.command, message,
)
logger.info("shell hook exited %d — blocking (event=%s command=%s): %s", BLOCK_EXIT_CODE, spec.event, spec.command, message)
return {"action": "block", "message": message}
# Other non-zero exits: still parse stdout so exit-code failures can carry a block directive.
if r["returncode"] != 0:
logger.warning(
"shell hook exited %d (event=%s command=%s); stderr=%s",
r["returncode"], spec.event, spec.command, stderr[:_STDERR_MESSAGE_LIMIT],
)
stdout = (r["stdout"] or "").strip()
parsed = _parse_response(spec.event, stdout)
if parsed is None and fail_closed and stdout and not _is_json_object(stdout):
# A fail-closed gate must not silently allow on garbage stdout (e.g. a stack trace).
return _fail_closed_block(spec, "unparseable stdout (expected a JSON object)")
@@ -457,35 +417,34 @@ def _block_message(primary: Any, secondary: Any) -> str:
return raw if isinstance(raw, str) and raw else _DEFAULT_BLOCK_MESSAGE
# pre_tool_call response dialects, checked in order: Hermes ``action`` then Claude-Code ``decision``.
# (verb key, block-message primary, block-message secondary, modify payload key) — the Claude-Code shape
# is translated to the canonical one expected by get_pre_tool_call_block_message.
_PRE_TOOL_DIALECTS = (("action", "message", "reason", "args"), ("decision", "reason", "message", "tool_input"))
def _parse_pre_tool_call(data: Dict[str, Any]) -> Optional[Dict[str, Any]]:
# Claude-Code-style {"decision": ..., "reason"/"tool_input": ...} is translated to the canonical
# Hermes shape expected by get_pre_tool_call_block_message.
if data.get("action") == "block":
return {"action": "block", "message": _block_message(data.get("message"), data.get("reason"))}
if data.get("decision") == "block":
return {"action": "block", "message": _block_message(data.get("reason"), data.get("message"))}
if data.get("action") == "modify" and isinstance(data.get("args"), dict):
return {"action": "modify", "args": data["args"]}
if data.get("decision") == "modify" and isinstance(data.get("tool_input"), dict):
return {"action": "modify", "args": data["tool_input"]}
for verb, primary, secondary, _ in _PRE_TOOL_DIALECTS:
if data.get(verb) == "block":
return {"action": "block", "message": _block_message(data.get(primary), data.get(secondary))}
for verb, _, _, payload in _PRE_TOOL_DIALECTS:
if data.get(verb) == "modify" and isinstance(data.get(payload), dict):
return {"action": "modify", "args": data[payload]}
return None
def _parse_pre_verify(data: Dict[str, Any]) -> Optional[Dict[str, Any]]:
# "continue" (Hermes) / "block" (Claude-Code Stop) both mean keep going; no message is a no-op.
action = str(data.get("action") or data.get("decision") or "").strip().lower()
if action in {"continue", "block"}:
message = data.get("message") or data.get("reason")
if isinstance(message, str) and message.strip():
return {"action": "continue", "message": message.strip()}
message = data.get("message") or data.get("reason")
if action in {"continue", "block"} and isinstance(message, str) and message.strip():
return {"action": "continue", "message": message.strip()}
return None
def _parse_context(data: Dict[str, Any]) -> Optional[Dict[str, Any]]:
context = data.get("context")
if isinstance(context, str) and context.strip():
return {"context": context}
return None
return {"context": context} if isinstance(context, str) and context.strip() else None
_RESPONSE_PARSERS: Dict[str, Callable[[Dict[str, Any]], Optional[Dict[str, Any]]]] = {
@@ -582,10 +541,8 @@ def _prompt_and_record(event: str, command: str, *, accept_hooks: bool) -> bool:
_record_approval(event, command)
logger.info("shell hook auto-approved via --accept-hooks / env / config: %s -> %s", event, command)
return True
if not sys.stdin.isatty():
return False
print(
f"\n⚠ Hermes is about to register a shell hook that will run a\n"
f" command on your behalf.\n\n"
@@ -599,7 +556,6 @@ def _prompt_and_record(event: str, command: str, *, accept_hooks: bool) -> bool:
except (EOFError, KeyboardInterrupt):
print() # keep the terminal tidy after ^C
return False
if answer not in {"y", "yes"}:
return False
_record_approval(event, command)
@@ -627,10 +583,7 @@ def revoke(command: str) -> int:
_SCRIPT_EXTENSIONS: Tuple[str, ...] = (
".sh", ".bash", ".zsh", ".fish",
".py", ".pyw",
".rb", ".pl", ".lua",
".js", ".mjs", ".cjs", ".ts",
".sh", ".bash", ".zsh", ".fish", ".py", ".pyw", ".rb", ".pl", ".lua", ".js", ".mjs", ".cjs", ".ts",
)
@@ -638,11 +591,9 @@ def _command_script_path(command: str) -> str:
"""Script path from ``command``: first token with a script extension, then a path-like token,
then the first token (``python3 /p/hook.py``, ``/usr/bin/env bash x.sh``)."""
try:
parts = _split(command)
parts = split_command_line(command) or [command]
except ValueError:
return command
if not parts:
return command
return (
next((p for p in parts if p.lower().endswith(_SCRIPT_EXTENSIONS)), None)
or next((p for p in parts if "/" in p or p.startswith("~")), None)
@@ -683,13 +634,11 @@ def script_is_executable(command: str) -> bool:
"""True iff ``command`` is runnable as configured: a bare script needs X_OK, an
interpreter-prefixed one only R_OK (mirrors what ``_spawn`` does)."""
path = _command_script_path(command)
if not path:
return False
expanded = os.path.expanduser(path)
if not os.path.isfile(expanded):
if not path or not os.path.isfile(expanded):
return False
try:
argv = _split(command)
argv = split_command_line(command)
except ValueError:
return False
is_bare_invocation = bool(argv) and argv[0] == path
+31 -56
View File
@@ -161,9 +161,7 @@ from tools.daemon_pool import DaemonThreadPoolExecutor as _DaemonExecutor
_EXECUTOR = _DaemonExecutor(max_workers=8, thread_name_prefix="hermes-lifecycle")
_SECRET = secrets.token_bytes(32)
_ACTIVE_PARENT_AGENT: contextvars.ContextVar[Any] = contextvars.ContextVar(
"hermes_subagent_lifecycle_parent", default=None
)
_ACTIVE_PARENT_AGENT: contextvars.ContextVar[Any] = contextvars.ContextVar("hermes_subagent_lifecycle_parent", default=None)
@contextmanager
@@ -193,6 +191,10 @@ def _finite_number(value: Any) -> bool:
return not isinstance(value, bool) and isinstance(value, (int, float)) and math.isfinite(value)
def _clip(value: Any) -> Optional[str]:
return str(value)[:_MAX_RESULT_CHARS] if value is not None else None
# Per-field shape check applied to a (possibly deserialized) handle before trusting it.
_HANDLE_FIELD_CHECKS: tuple[tuple[str, Callable[[Any], bool]], ...] = (
("contract_version", lambda v: type(v) is int and v == PUBLIC_CONTRACT_VERSION),
@@ -207,11 +209,15 @@ _HANDLE_FIELD_CHECKS: tuple[tuple[str, Callable[[Any], bool]], ...] = (
("capability", lambda v: isinstance(v, str)),
)
# Launch-request fields the public contract deliberately rejects: (predicate, error).
_UNSUPPORTED_REQUEST_FIELDS: tuple[tuple[Callable[[SubagentLaunchRequest], bool], str], ...] = (
(lambda r: r.timeout_seconds is not None,
"Per-launch timeout is not supported; configure delegation timeout explicitly."),
# Launch-request rejections in check order: (predicate, error). The type check leads so later
# predicates may dereference request fields.
_REQUEST_REJECTIONS: tuple[tuple[Callable[[Any], bool], str], ...] = (
(lambda r: not isinstance(r, SubagentLaunchRequest) or not isinstance(r.goal, str) or not r.goal.strip() or len(r.goal) > _MAX_GOAL_CHARS,
"goal must be a non-empty string of at most 16000 characters."),
(lambda r: r.context is not None and (not isinstance(r.context, str) or len(r.context) > _MAX_CONTEXT_CHARS),
"context must be a string of at most 32000 characters."),
(lambda r: r.role not in {"leaf", "orchestrator"}, "role must be 'leaf' or 'orchestrator'."),
(lambda r: r.timeout_seconds is not None, "Per-launch timeout is not supported; configure delegation timeout explicitly."),
(lambda r: r.working_directory is not None,
"working_directory is not supported because Hermes delegates use isolated task environments."),
(lambda r: bool(r.blocked_tools),
@@ -220,9 +226,7 @@ _UNSUPPORTED_REQUEST_FIELDS: tuple[tuple[Callable[[SubagentLaunchRequest], bool]
def _handle_is_well_formed(handle: Any) -> bool:
return isinstance(handle, SubagentHandle) and all(
check(getattr(handle, field)) for field, check in _HANDLE_FIELD_CHECKS
)
return isinstance(handle, SubagentHandle) and all(check(getattr(handle, field)) for field, check in _HANDLE_FIELD_CHECKS)
class SubagentLifecycleService:
@@ -246,36 +250,22 @@ class SubagentLifecycleService:
self._cleanup_locked()
if request.correlation_id and correlation_key in _REGISTRY.correlations:
raise SubagentLifecycleError("Duplicate correlation_id for this parent session.")
# Delegate construction stays internal: plugins never import private delegation helpers.
from tools.delegate_tool import _build_child_preserving_parent_tools, DEFAULT_MAX_ITERATIONS
child = _build_child_preserving_parent_tools(
task_index=0,
goal=request.goal,
context=request.context,
task_index=0, goal=request.goal, context=request.context,
toolsets=list(request.allowed_toolsets) if request.allowed_toolsets else None,
model=request.model,
max_iterations=DEFAULT_MAX_ITERATIONS,
task_count=1,
parent_agent=parent,
role=request.role,
model=request.model, max_iterations=DEFAULT_MAX_ITERATIONS, task_count=1, parent_agent=parent, role=request.role,
)
subagent_id = str(getattr(child, "_subagent_id", "") or "")
if not subagent_id:
raise SubagentLifecycleError("Hermes failed to assign a child identity.")
created = time.time()
handle = SubagentHandle(
PUBLIC_CONTRACT_VERSION,
subagent_id,
parent_session_id,
request.correlation_id,
created,
getattr(child, "provider", None),
getattr(child, "model", None),
getattr(child, "_delegate_role", request.role),
int(getattr(child, "_delegate_depth", 1) or 1),
self._capability(subagent_id, parent_session_id, created),
PUBLIC_CONTRACT_VERSION, subagent_id, parent_session_id, request.correlation_id, created,
getattr(child, "provider", None), getattr(child, "model", None), getattr(child, "_delegate_role", request.role),
int(getattr(child, "_delegate_depth", 1) or 1), self._capability(subagent_id, parent_session_id, created),
)
record = _Record(handle, SubagentState.PENDING, created, agent=child)
with _REGISTRY.lock:
@@ -296,10 +286,9 @@ class SubagentLifecycleService:
record = self._record(handle)
if record is None:
return SubagentTerminalState(handle, SubagentState.UNKNOWN, True, diagnostic="UNKNOWN_HANDLE")
future = record.future
if future is not None:
if record.future is not None:
try:
future.result(timeout=timeout_seconds)
record.future.result(timeout=timeout_seconds)
except TimeoutError:
return SubagentTerminalState(record.handle, record.state, False, True)
except Exception:
@@ -321,9 +310,7 @@ class SubagentLifecycleService:
if agent is not None:
try:
accepted = request_hard_interrupt(
agent,
f"Lifecycle cancellation requested: {reason[:500]}",
tool_reason="subagent cancellation requested",
agent, f"Lifecycle cancellation requested: {reason[:500]}", tool_reason="subagent cancellation requested",
)
except Exception:
accepted = False
@@ -344,6 +331,7 @@ class SubagentLifecycleService:
return SubagentReconnectResult(True, record.state)
def _record(self, handle: SubagentHandle) -> Optional[_Record]:
"""Registry record for a well-formed, capability-verified handle owned by the active parent."""
if not _handle_is_well_formed(handle):
return None
expected = self._capability(handle.subagent_id, handle.parent_session_id, handle.created_at)
@@ -371,8 +359,7 @@ class SubagentLifecycleService:
with _REGISTRY.lock:
if record.state is not SubagentState.CANCEL_REQUESTED:
record.state = SubagentState.RUNNING
record.started_at = time.time()
record.updated_at = record.started_at
record.started_at = record.updated_at = time.time()
try:
from tools.delegate_tool import _run_child_lifecycle
@@ -381,25 +368,20 @@ class SubagentLifecycleService:
raw = raw if is_dict else {}
status = str(raw.get("status", "error"))
if status == "interrupted":
cancelled = record.state == SubagentState.CANCEL_REQUESTED
state = SubagentState.CANCELLED if cancelled else SubagentState.INTERRUPTED
state = SubagentState.CANCELLED if record.state == SubagentState.CANCEL_REQUESTED else SubagentState.INTERRUPTED
else:
state = SubagentState.SUCCEEDED if status == "completed" else SubagentState.FAILED
summary = raw.get("summary")
error = raw.get("error")
fields: dict[str, Any] = dict(
summary=str(summary)[:_MAX_RESULT_CHARS] if summary is not None else None,
summary=_clip(raw.get("summary")),
error_classification=None if state == SubagentState.SUCCEEDED else status.upper(),
error_message=str(error)[:_MAX_RESULT_CHARS] if error else None,
error_message=_clip(raw.get("error") or None),
usage_metadata={"api_calls": raw.get("api_calls", 0)} if is_dict else {},
tool_execution_summary={"duration_seconds": raw.get("duration_seconds", 0)} if is_dict else {},
)
except Exception as exc:
state = SubagentState.FAILED
fields = dict(error_classification=type(exc).__name__, error_message=str(exc)[:_MAX_RESULT_CHARS])
result = SubagentResult(
record.handle, state, True, started_at=record.started_at, completed_at=time.time(), **fields
)
fields = dict(error_classification=type(exc).__name__, error_message=_clip(exc))
result = SubagentResult(record.handle, state, True, started_at=record.started_at, completed_at=time.time(), **fields)
payload = dataclasses.asdict(result)
payload.pop("result_hash", None)
digest = hashlib.sha256(json.dumps(payload, sort_keys=True, default=str).encode()).hexdigest()
@@ -418,14 +400,7 @@ class SubagentLifecycleService:
@staticmethod
def _validate_request(request: SubagentLaunchRequest, parent: Any) -> None:
goal_ok = isinstance(request, SubagentLaunchRequest) and isinstance(request.goal, str)
if not goal_ok or not request.goal.strip() or len(request.goal) > _MAX_GOAL_CHARS:
raise SubagentLifecycleError("goal must be a non-empty string of at most 16000 characters.")
if request.context is not None and (not isinstance(request.context, str) or len(request.context) > _MAX_CONTEXT_CHARS):
raise SubagentLifecycleError("context must be a string of at most 32000 characters.")
if request.role not in {"leaf", "orchestrator"}:
raise SubagentLifecycleError("role must be 'leaf' or 'orchestrator'.")
for rejected, message in _UNSUPPORTED_REQUEST_FIELDS:
for rejected, message in _REQUEST_REJECTIONS:
if rejected(request):
raise SubagentLifecycleError(message)
try:
+10 -52
View File
@@ -22,11 +22,9 @@ logger = logging.getLogger(__name__)
# (task_name, exception) -> None; surfaces auxiliary failures so silent drops don't pile up as NULL titles.
FailureCallback = Callable[[str, BaseException], None]
# (title, source) -> None; source is the persisted provenance (``derived`` / ``llm``). Consumers paying a
# rate-limited remote rename per title (Discord thread, Telegram topic) should act on ``llm`` only.
TitleCallback = Callable[[str, str], None]
# () -> bool, called right before the LLM request; False skips (e.g. the user switched models and
# the request would reload one the runtime already evicted).
RuntimeValidator = Callable[[], bool]
@@ -68,12 +66,7 @@ _TITLE_RESPONSE_FORMAT = {
"json_schema": {
"name": "session_title",
"strict": True,
"schema": {
"type": "object",
"properties": {"title": {"type": "string"}},
"required": ["title"],
"additionalProperties": False,
},
"schema": {"type": "object", "properties": {"title": {"type": "string"}}, "required": ["title"], "additionalProperties": False},
},
}
@@ -81,20 +74,13 @@ _TITLE_RESPONSE_FORMAT = {
# RECOGNIZED_CONTROL_WRAPPERS): stripped, titling continues on what remains.
_CONTROL_WRAPPERS = tuple(
(f"<{tag}>", f"</{tag}>")
for tag in (
"command-message", "command-name", "command-args", "local-command-caveat",
"local-command-stderr", "local-command-stdout", "task-notification",
"system-reminder", "ide_opened_file", "ide_selection",
)
for tag in ("command-message", "command-name", "command-args", "local-command-caveat", "local-command-stderr",
"local-command-stdout", "task-notification", "system-reminder", "ide_opened_file", "ide_selection")
)
# Hermes' own machine-authored openers: a compaction handoff or resumed session must not be titled after them.
_MACHINE_PREFIXES = (
"[CONTEXT COMPACTION",
LEGACY_SUMMARY_PREFIX,
"[Runtime note:",
"[System note:",
"[SYSTEM]",
"[CONTEXT COMPACTION", LEGACY_SUMMARY_PREFIX, "[Runtime note:", "[System note:", "[SYSTEM]",
# tui_gateway.server._MODEL_SWITCH_MARKER_PREFIX (keep in sync); persisted as role="user" because
# strict providers reject a non-first system message.
"[System: The active model for this chat has changed to ",
@@ -175,8 +161,6 @@ def is_titleable_user_message(user_message: str) -> bool:
def derive_title(user_message: str) -> Optional[str]:
"""Instant title: first meaningful line trimmed to a word boundary. No model, never fails."""
line = " ".join(_first_line(_summarize_user_message(user_message)).split())
if not line:
return None
if len(line) > MAX_DERIVED_TITLE_CHARS:
cut = line[:MAX_DERIVED_TITLE_CHARS]
space = cut.rfind(" ")
@@ -226,11 +210,9 @@ def _extract_title_text(content: str) -> str:
def _clean_title(text: str) -> Optional[str]:
"""Normalize a model-produced title, or None when nothing usable remains."""
title = _strip_title_prefix(" ".join((text or "").split()).strip("\"'").strip()).rstrip(".!,;:")
if not title:
return None
if len(title) > 80:
title = title[:77].rstrip() + "..."
return title
return title or None
def _safe_callback(callback: Optional[Callable], args: tuple, log_fmt: str, label: str) -> None:
@@ -263,7 +245,6 @@ def generate_title(
if not _auto_title_enabled():
logger.debug("Auto-title skipped: auxiliary.title_generation.enabled=false")
return None
if runtime_validator is not None:
try:
if not runtime_validator():
@@ -272,16 +253,13 @@ def generate_title(
except Exception:
# Fail open: a broken validator must not disable titling.
logger.debug("Title runtime validator raised; proceeding", exc_info=True)
user_snippet = _summarize_user_message(user_message)[:MAX_TITLE_INPUT_CHARS]
if not user_snippet.strip():
return None
language = _title_language()
language_rule = _LANGUAGE_RULE_PINNED.format(language=language) if language else _LANGUAGE_RULE_MATCH_USER
# str.replace, not str.format: the prompt embeds literal JSON braces.
prompt = _TITLE_PROMPT_TEMPLATE.replace("__LANGUAGE_RULE__", language_rule)
try:
response = call_llm(
task="title_generation",
@@ -332,8 +310,7 @@ def _persist_session_title(session_db, session_id, title, *, source, dedupe=True
return candidate
logger.debug("Skipping %s title: a higher-authority title already holds session %s", source, session_id)
return None
# Older store without provenance support.
legacy_fn = getattr(session_db, "set_auto_title_if_empty", None)
legacy_fn = getattr(session_db, "set_auto_title_if_empty", None) # older store without provenance
if legacy_fn is not None:
return candidate if legacy_fn(session_id, candidate) else None
if session_db.set_session_title(session_id, candidate) is False:
@@ -352,20 +329,13 @@ def _persist_session_title(session_db, session_id, title, *, source, dedupe=True
return _set(deduped)
def apply_instant_title(
session_db,
session_id: str,
user_message: str,
title_callback: Optional[TitleCallback] = None,
) -> Optional[str]:
def apply_instant_title(session_db, session_id: str, user_message: str, title_callback: Optional[TitleCallback] = None) -> Optional[str]:
"""Write the derived title synchronously (cheap enough to run inline). Returns the title
written, or None (no usable text, or a title of at least ``derived`` authority exists). Never raises."""
if not session_db or not session_id:
return None
try:
if not is_titleable_user_message(user_message):
return None
title = derive_title(user_message)
title = derive_title(user_message) if is_titleable_user_message(user_message) else None
if not title:
return None
persisted = _persist_session_title(session_db, session_id, title, source="derived", dedupe=False)
@@ -394,7 +364,6 @@ def auto_title_session(
# A derived title is expected here — upgrading it is the point.
if not session_db or not session_id or _has_upgraded_title(session_db, session_id):
return
# This daemon thread starts AFTER the turn's ambient conversation context was reset;
# republish it so the title call carries the same Portal ``conversation=`` tag
# (root-of-lineage) and bills usage to this session.
@@ -406,10 +375,8 @@ def auto_title_session(
conversation_id = session_db.get_conversation_root(session_id) or session_id
set_conversation_context(conversation_id)
set_accounting_context(session_db, session_id)
title = generate_title(
user_message, failure_callback=failure_callback,
main_runtime=main_runtime, runtime_validator=runtime_validator,
user_message, failure_callback=failure_callback, main_runtime=main_runtime, runtime_validator=runtime_validator,
)
source = "llm"
if not title:
@@ -418,7 +385,6 @@ def auto_title_session(
title, source = derive_title(user_message), "derived"
if not title:
return
try:
persisted = _persist_session_title(session_db, session_id, title, source=source)
if persisted is None:
@@ -468,30 +434,22 @@ def maybe_auto_title(
Call at the START of a turn, before the model is invoked."""
if not session_db or not session_id or not user_message:
return
# History may be pre- or post-message. Skip only when BOTH past the opening turn AND already named:
# count alone left a machinery-opened session nameless; title alone never titles on an old store.
user_msg_count = sum(1 for m in (conversation_history or []) if _is_real_user_turn(m))
if user_msg_count > 1 and not _session_is_untitled(session_db, session_id):
return
if not is_titleable_user_message(user_message):
return
# Config read after the cheap guards so the file isn't touched every turn.
if not _auto_title_enabled():
logger.debug("Auto-title skipped: auxiliary.title_generation.enabled=false")
return
apply_instant_title(session_db, session_id, user_message, title_callback)
threading.Thread(
target=auto_title_session,
args=(session_db, session_id, user_message),
kwargs=dict(
failure_callback=failure_callback, main_runtime=main_runtime,
title_callback=title_callback, runtime_validator=runtime_validator,
),
kwargs=dict(failure_callback=failure_callback, main_runtime=main_runtime, title_callback=title_callback, runtime_validator=runtime_validator),
daemon=True,
name="auto-title",
).start()
+8 -43
View File
@@ -36,6 +36,7 @@ _NO_TOKEN_MESSAGE = (
" HF_TOKEN=hf_xxxxxxxxxxxxxxxxxxxx\n"
"3. Run /upload-trace again (or `hermes trace upload`)."
)
_TOKEN_ENV_VARS = ("HF_TOKEN", "HUGGINGFACE_HUB_TOKEN", "HUGGING_FACE_HUB_TOKEN", "HUGGINGFACE_TOKEN")
class TraceRedactionError(RuntimeError):
@@ -80,11 +81,9 @@ def _content_to_blocks(content: Any, redact: bool) -> List[Dict[str, Any]]:
"""Normalize a message ``content`` field into Anthropic content blocks."""
if content is None:
return []
if isinstance(content, str):
return [_text_block(content, redact)]
if isinstance(content, list):
return [_part_to_block(part, redact) for part in content]
return [_text_block(json.dumps(content), redact)]
return [_text_block(content if isinstance(content, str) else json.dumps(content), redact)]
def _parse_tool_args(raw_args: Any) -> Dict[str, Any]:
@@ -152,10 +151,7 @@ def _tool_result_message(msg: Dict[str, Any], model: str, redact: bool) -> Dict[
def _user_message(msg: Dict[str, Any], model: str, redact: bool) -> Dict[str, Any]:
content = msg.get("content")
return {
"role": "user",
"content": _redact(content, redact) if isinstance(content, str) else _content_to_blocks(content, redact),
}
return {"role": "user", "content": _redact(content, redact) if isinstance(content, str) else _content_to_blocks(content, redact)}
# role -> (Claude Code line type, message builder). Unknown roles render as user.
@@ -165,14 +161,7 @@ _ROLE_RENDERERS: Dict[Any, Tuple[str, Any]] = {
}
def build_trace_jsonl(
messages: List[Dict[str, Any]],
*,
session_id: str,
model: str = "",
cwd: str = "",
redact: bool = True,
) -> str:
def build_trace_jsonl(messages: List[Dict[str, Any]], *, session_id: str, model: str = "", cwd: str = "", redact: bool = True) -> str:
"""Render messages as Claude Code JSONL: one line per non-system message (``user``/``tool`` ->
type user, ``assistant`` -> type assistant with text + ``tool_use`` blocks; tool results ride
on user turns as ``tool_result`` keyed by ``tool_call_id``; turns link via ``parentUuid``)."""
@@ -180,7 +169,6 @@ def build_trace_jsonl(
parent: Optional[str] = None
base_ts = _now_iso()
git_branch = _git_branch(cwd)
for msg in messages:
role = msg.get("role")
if role == "system":
@@ -202,7 +190,6 @@ def build_trace_jsonl(
}
lines.append(json.dumps(entry, ensure_ascii=False))
parent = turn_uuid
return "\n".join(lines) + ("\n" if lines else "")
@@ -210,21 +197,10 @@ def build_trace_jsonl(
def _resolve_hf_token() -> Optional[str]:
"""Return the user's Hugging Face token from the usual env vars."""
for var in ("HF_TOKEN", "HUGGINGFACE_HUB_TOKEN", "HUGGING_FACE_HUB_TOKEN", "HUGGINGFACE_TOKEN"):
val = (os.getenv(var) or "").strip()
if val:
return val
return None
return next((val for var in _TOKEN_ENV_VARS if (val := (os.getenv(var) or "").strip())), None)
def _do_upload(
jsonl: str,
*,
token: str,
session_id: str,
dataset_name: str = DEFAULT_DATASET_NAME,
private: bool = True,
) -> str:
def _do_upload(jsonl: str, *, token: str, session_id: str, dataset_name: str = DEFAULT_DATASET_NAME, private: bool = True) -> str:
"""Create (idempotently) the private dataset and push the trace file.
Returns a user-facing status string. Never raises."""
with suppress(Exception): # lazy-install unavailable/declined — the import below surfaces the hint
@@ -234,38 +210,30 @@ def _do_upload(
from huggingface_hub import HfApi
except ImportError:
return "Hugging Face upload needs the `huggingface_hub` package (`pip install huggingface_hub`)."
api = HfApi(token=token)
try:
who = api.whoami()
user = who.get("name") if isinstance(who, dict) else None
except Exception as e:
logger.warning("HF whoami failed: %s", e)
return ("Your Hugging Face token was rejected (whoami failed). "
"Make sure it has WRITE access and isn't expired.")
return "Your Hugging Face token was rejected (whoami failed). Make sure it has WRITE access and isn't expired."
if not user:
return "Could not resolve your Hugging Face username from the token."
repo_id = f"{user}/{dataset_name}"
try:
api.create_repo(repo_id=repo_id, repo_type="dataset", private=private, exist_ok=True)
except Exception as e:
logger.warning("HF create_repo failed for %s: %s", repo_id, e)
return f"Could not create/access dataset {repo_id}: {e}"
path_in_repo = f"sessions/{session_id}.jsonl"
try:
api.upload_file(
path_or_fileobj=jsonl.encode("utf-8"),
path_in_repo=path_in_repo,
repo_id=repo_id,
repo_type="dataset",
path_or_fileobj=jsonl.encode("utf-8"), path_in_repo=path_in_repo, repo_id=repo_id, repo_type="dataset",
commit_message=f"add session trace {session_id}",
)
except Exception as e:
logger.warning("HF upload_file failed for %s: %s", repo_id, e)
return f"Upload to Hugging Face failed: {e}"
return (f"Uploaded -> https://huggingface.co/datasets/{repo_id}/blob/main/{path_in_repo}\n"
f"View in the trace viewer: https://huggingface.co/datasets/{repo_id}")
@@ -301,11 +269,9 @@ def upload_session_trace(
dataset. Returns a status string, never raises."""
if not session_id:
return "No active session to upload."
token = token or _resolve_hf_token()
if not token:
return _NO_TOKEN_MESSAGE
try:
messages, meta = load_session_messages(session_id, db_path=db_path)
except Exception as e:
@@ -319,5 +285,4 @@ def upload_session_trace(
return _REDACTION_BLOCKED_MESSAGE
if not jsonl.strip():
return "No transcript content to upload for this session."
return _do_upload(jsonl, token=token, session_id=session_id, dataset_name=dataset_name, private=private)