Skip to content

The agent that grows with you

refactor(agent/title_generator): unify title-prefix strip, compact docstrings

Tekniumc01255c90bcf
diff --git a/agent/title_generator.py b/agent/title_generator.py
index 48ac92795e6..47aa2fee2a6 100644
--- a/agent/title_generator.py
+++ b/agent/title_generator.py
@@ -1,11 +1,10 @@
"""Auto-generate short session titles from the user's opening message.
-Two stages, both off the critical path: an **instant** deterministic title
-derived from the first user message (written before the model is called, cannot
-fail), then an **upgrade** from one small-model call (cheap tier, thinking off,
-JSON-constrained response). Provenance ``derived < llm < user`` is enforced by
-the storage layer, so stage 2 only replaces stage 1 and neither replaces a name
-the user typed.
+Two stages, both off the critical path: an **instant** deterministic title derived from the first
+user message (written before the model is called, cannot fail), then an **upgrade** from one
+small-model call (cheap tier, thinking off, JSON-constrained). Provenance ``derived < llm < user``
+is enforced by the storage layer, so stage 2 only replaces stage 1 and neither replaces a name the
+user typed.
"""
import json
@@ -20,25 +19,25 @@ from agent.message_content import flatten_message_text
logger = logging.getLogger(__name__)
-# (task_name, exception) -> None; surfaces auxiliary failures to the user
-# (AIAgent._emit_auxiliary_failure) so silent drops don't pile up as NULL titles.
+# (task_name, exception) -> None; surfaces auxiliary failures to the user 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; a local sidebar wants both.
+# (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; a local sidebar wants both.
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).
+# () -> 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]
# Text budget handed to the model (Claude Code / OpenClaw converged on 1000).
MAX_TITLE_INPUT_CHARS = 1000
# Cap on the instant derived title; a raw fragment reads worse the longer it runs.
MAX_DERIVED_TITLE_CHARS = 48
-# Answer-shaped guard: a tiny model sometimes answers the user instead of
-# titling; more words than this is rejected rather than truncated and stored.
+# Answer-shaped guard: a tiny model sometimes answers the user instead of titling; more words
+# than this is rejected rather than truncated and stored.
_MAX_TITLE_WORDS = 12
_TITLE_PROMPT_TEMPLATE = (
@@ -65,8 +64,8 @@ _TITLE_PROMPT_TEMPLATE = (
_LANGUAGE_RULE_MATCH_USER = "- Write the title in the same language as the user's message."
_LANGUAGE_RULE_PINNED = "- Write the title in {language}."
-# Constrains the response to a single title field, removing the whole class of
-# "model answered instead of titling" failures seen in real session history.
+# Constrains the response to a single title field, removing the "model answered instead of
+# titling" failure class.
_TITLE_RESPONSE_FORMAT = {
"type": "json_schema",
"json_schema": {
@@ -81,9 +80,8 @@ _TITLE_RESPONSE_FORMAT = {
},
}
-# Control-tag wrappers around machine-authored content inside a nominal "user"
-# message (ported from Codex CLI's RECOGNIZED_CONTROL_WRAPPERS): stripped, and
-# titling continues on what remains, rather than refusing outright.
+# Control-tag wrappers around machine-authored content inside a nominal "user" message (ported
+# from Codex CLI's RECOGNIZED_CONTROL_WRAPPERS): stripped, and titling continues on what remains.
_CONTROL_WRAPPERS = tuple(
(f"<{tag}>", f"</{tag}>")
for tag in (
@@ -93,24 +91,23 @@ _CONTROL_WRAPPERS = tuple(
)
)
-# Hermes' own machine-authored openers: a compaction handoff or resumed session
-# must not be titled after its scaffolding.
+# Hermes' own machine-authored openers: a compaction handoff or resumed session must not be
+# titled after its scaffolding.
_MACHINE_PREFIXES = (
"[CONTEXT COMPACTION",
LEGACY_SUMMARY_PREFIX,
"[Runtime note:",
"[System note:",
"[SYSTEM]",
- # Model-switch marker (tui_gateway.server._MODEL_SWITCH_MARKER_PREFIX, keep in
- # sync). Persisted with role="user" because strict providers reject a
- # non-first system message, so without this it looks like a real opener.
+ # Model-switch marker (tui_gateway.server._MODEL_SWITCH_MARKER_PREFIX, keep in sync). Persisted
+ # with role="user" because strict providers reject a non-first system message.
"[System: The active model for this chat has changed to ",
)
def _title_config() -> dict:
- """``auxiliary.title_generation`` from config. Lazy read-only import: avoids
- hermes_cli circularity and config-migration writes."""
+ """``auxiliary.title_generation`` from config. Lazy read-only import: avoids hermes_cli
+ circularity and config-migration writes."""
from hermes_cli.config import load_config_readonly
return ((load_config_readonly() or {}).get("auxiliary") or {}).get("title_generation") or {}
@@ -135,8 +132,8 @@ def _auto_title_enabled() -> bool:
def strip_control_wrappers(text: str) -> str:
- """Remove leading control wrappers, including nested ones, so a slash-command
- turn reduces to the prose the user typed (still titleable, unlike a refusal)."""
+ """Remove leading control wrappers, including nested ones, so a slash-command turn reduces to
+ the prose the user typed (still titleable, unlike a refusal)."""
if not text:
return ""
current = text.strip()
@@ -163,8 +160,8 @@ def strip_control_wrappers(text: str) -> str:
def _summarize_user_message(user_message: str) -> str:
- """Reduce a user turn to the text worth titling: a ``/skill`` invocation embeds
- the whole skill body, so parse the scaffolding first, then strip wrappers."""
+ """Reduce a user turn to the text worth titling: a ``/skill`` invocation embeds the whole
+ skill body, so parse the scaffolding first, then strip wrappers."""
if not user_message:
return ""
described = None
@@ -178,8 +175,8 @@ def _summarize_user_message(user_message: str) -> str:
def is_titleable_user_message(user_message: str) -> bool:
- """False for machine-authored openers and turns that reduce to nothing once
- control scaffolding is stripped."""
+ """False for machine-authored openers and turns that reduce to nothing once control
+ scaffolding is stripped."""
if not isinstance(user_message, str) or not user_message.strip():
return False
if user_message.lstrip().startswith(_MACHINE_PREFIXES):
@@ -188,8 +185,8 @@ 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; its job is to beat the model to the screen, not on quality."""
+ """Instant title: first meaningful line trimmed to a word boundary. No model, never fails;
+ its job is to beat the model to the screen, not on quality."""
text = _summarize_user_message(user_message)
if not text:
return None
@@ -206,9 +203,13 @@ def derive_title(user_message: str) -> Optional[str]:
return line or None
+def _strip_title_prefix(text: str) -> str:
+ return text[6:].strip() if text.lower().startswith("title:") else text
+
+
def _extract_title_text(content: str) -> str:
- """Pull the title out of a model response: strict JSON, then a loose JSON scan,
- then first-line prose so a provider ignoring ``response_format`` still titles."""
+ """Pull the title out of a model response: strict JSON, then a loose JSON scan, then
+ first-line prose so a provider ignoring ``response_format`` still titles."""
if not content:
return ""
raw = content.strip()
@@ -235,17 +236,12 @@ def _extract_title_text(content: str) -> str:
except Exception:
logger.debug("strip_think_blocks unavailable for title output", exc_info=True)
raw = next((ln.strip() for ln in raw.splitlines() if ln.strip()), "")
- if raw.lower().startswith("title:"):
- raw = raw[6:].strip()
- return raw.strip("\"'").strip()
+ return _strip_title_prefix(raw).strip("\"'").strip()
def _clean_title(text: str) -> Optional[str]:
"""Normalize a model-produced title, or None when nothing usable remains."""
- title = " ".join((text or "").split()).strip("\"'").strip()
- if title.lower().startswith("title:"):
- title = title[6:].strip()
- title = title.rstrip(".!,;:")
+ title = _strip_title_prefix(" ".join((text or "").split()).strip("\"'").strip()).rstrip(".!,;:")
if not title:
return None
if len(title) > 80:
@@ -278,8 +274,8 @@ def generate_title(
main_runtime: dict = None,
runtime_validator: Optional[RuntimeValidator] = None,
) -> Optional[str]:
- """Generate a session title from the user's opening message alone (waiting for
- the assistant made this slow and bought nothing).
+ """Generate a session title from the user's opening message alone (waiting for the assistant
+ made this slow and bought nothing).
``failure_callback`` gets ``(task, exception)`` when the auxiliary call raises;
``runtime_validator`` runs right before the request and False skips silently.
@@ -329,14 +325,13 @@ def generate_title(
def _persist_session_title(session_db, session_id, title, *, source, dedupe=True):
- """Persist a title at *source* authority via ``set_auto_title`` (precedence
- check + write in one transaction, so a manual ``/title`` is never overwritten).
+ """Persist a title at *source* authority via ``set_auto_title`` (precedence check + write in
+ one transaction, so a manual ``/title`` is never overwritten).
- ``ValueError`` means the unique-title index rejected the name; append ``#N``
- via ``get_next_title_in_lineage``. ``dedupe=False`` re-raises instead: the
- derived title is on the turn's critical path, collides constantly ("hi"), and
- the lineage scan is a widening scan for a name the model replaces a second
- later — the background stage picks the collision back up.
+ ``ValueError`` means the unique-title index rejected the name; append ``#N`` via
+ ``get_next_title_in_lineage``. ``dedupe=False`` re-raises instead: the derived title is on the
+ turn's critical path, collides constantly ("hi"), and the widening lineage scan is wasted on a
+ name the model replaces a second later — the background stage picks the collision back up.
Returns the persisted title, or None when a higher-authority title held the row.
"""
@@ -376,8 +371,8 @@ def apply_instant_title(
) -> Optional[str]:
"""Write the derived title synchronously (cheap enough to run inline).
- Returns the title written, or None when nothing was (no usable text, or a
- title of at least ``derived`` authority exists). Never raises.
+ Returns the title written, or None when nothing was (no usable text, or a title of at least
+ ``derived`` authority exists). Never raises.
"""
if not session_db or not session_id:
return None
@@ -407,11 +402,10 @@ def auto_title_session(
) -> None:
"""Generate and store the model title (daemon-thread target).
- Skips when the session already carries an ``llm``/``user`` title. Never lets
- an exception escape (the default threading excepthook would spray a raw
- traceback into the terminal); the canonical trigger is the post-``hermes
- update`` stale-module window, where lazy imports read NEW source against OLD
- cached modules until the process restarts.
+ Skips when the session already carries an ``llm``/``user`` title. Never lets an exception
+ escape (the default threading excepthook would spray a raw traceback into the terminal); the
+ canonical trigger is the post-``hermes update`` stale-module window, where lazy imports read
+ NEW source against OLD cached modules until the process restarts.
"""
try:
if not session_db or not session_id:
@@ -427,9 +421,9 @@ def auto_title_session(
except Exception:
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.
+ # 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.
from agent.aux_accounting import set_accounting_context
from agent.portal_tags import set_conversation_context
@@ -447,8 +441,8 @@ def auto_title_session(
)
source = "llm"
if not title:
- # The inline attempt declines collisions rather than scan the lineage
- # on the critical path; off that path the scan is affordable.
+ # The inline attempt declines collisions rather than scan the lineage on the critical
+ # path; off that path the scan is affordable.
title = derive_title(user_message)
source = "derived"
if not title:
@@ -470,8 +464,8 @@ def auto_title_session(
def _is_real_user_turn(message: Any) -> bool:
- """Whether a history entry is a question a person actually asked (Hermes
- persists machinery under ``role="user"``); a multimodal turn is judged on its text."""
+ """Whether a history entry is a question a person actually asked (Hermes persists machinery
+ under ``role="user"``); a multimodal turn is judged on its text."""
if not isinstance(message, dict) or message.get("role") != "user":
return False
content = message.get("content")
@@ -479,8 +473,8 @@ def _is_real_user_turn(message: Any) -> bool:
def _session_is_untitled(session_db, session_id: str) -> bool:
- """Whether the session carries no title of any provenance. False when it can't
- tell: an unreadable title is no reason to spend a model call per turn."""
+ """Whether the session carries no title of any provenance. False when it can't tell: an
+ unreadable title is no reason to spend a model call per turn."""
getter = getattr(session_db, "get_session_title", None)
if not callable(getter):
return False
@@ -501,15 +495,14 @@ def maybe_auto_title(
title_callback: Optional[TitleCallback] = None,
runtime_validator: Optional[RuntimeValidator] = None,
) -> None:
- """Title a session from its opening message: instant inline, then upgraded on
- a daemon thread. Call at the START of a turn, before the model is invoked."""
+ """Title a session from its opening message: instant inline, then upgraded on a daemon
+ thread. 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 depending on the caller, so accept both.
- # Skip only when BOTH past the opening turn AND already named: count alone left
- # a session that opened with machinery nameless; title alone never titles on a
- # store too old to report one.
+ # History may be pre- or post-message depending on the caller. Skip only when BOTH past the
+ # opening turn AND already named: count alone left a session that opened with machinery
+ # nameless; title alone never titles on a store too old to report one.
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