Hermes Agent 命令行操作界面
2026-07-15 3401541
2026-07-15 0
Hermes Agent 实现了一个智能的上下文压缩系统,用于处理长对话时的上下文窗口限制问题。压缩算法包含以下步骤:

def should_compress(self, prompt_tokens: int = None) -> bool:
"""Check if context exceeds the compression threshold."""
tokens = prompt_tokens if prompt_tokens is not None else self.last_prompt_tokens
return tokens >= self.threshold_tokens
压缩阈值默认为模型上下文长度的 50%,但会设置一个最小值(MINIMUM_CONTEXT_LENGTH)。
def _prune_old_tool_results(self, messages: List[Dict[str, Any]], protect_tail_count: int,
protect_tail_tokens: int | None = None) -> tuple[List[Dict[str, Any]], int]:
"""Replace old tool result contents with a short placeholder."""
# 实现细节...
这是一个廉价的预处理步骤,无需 LLM 调用,通过替换旧工具结果为占位符来减少上下文大小。
def _find_tail_cut_by_tokens(self, messages: List[Dict[str, Any]], head_end: int,
token_budget: int | None = None) -> int:
"""Walk backward from the end of messages, accumulating tokens until the budget is reached."""
# 实现细节...
使用令牌预算而非固定消息数来保护尾部消息,确保最近的重要上下文得以保留。
def _generate_summary(self, turns_to_summarize: List[Dict[str, Any]], focus_topic: str = None) -> Optional[str]:
"""Generate a structured summary of conversation turns."""
# 实现细节...
使用结构化模板生成摘要,包括:
def compress(self, messages: List[Dict[str, Any]], current_tokens: int = None, focus_topic: str = None) -> List[Dict[str, Any]]:
"""Compress conversation messages by summarizing middle turns."""
# 实现细节...
这是压缩的主入口点,协调所有压缩步骤并返回压缩后的消息列表。
在 run_agent.py 中,_compress_context 方法处理压缩后的会话管理:
def _compress_context(self, messages: list, system_message: str, *, approx_tokens: int = None, task_id: str = "default", focus_topic: str = None) -> tuple:
"""Compress conversation context and split the session in SQLite."""
# 预压缩内存刷新
self.flush_memories(messages, min_turns=0)
# 通知外部内存提供者
if self._memory_manager:
try:
self._memory_manager.on_pre_compress(messages)
except Exception:
pass
# 执行压缩
compressed = self.context_compressor.compress(messages, current_tokens=approx_tokens, focus_topic=focus_topic)
# 会话分割与管理
if self._session_db:
try:
# 传播标题到新会话并自动编号
old_title = self._session_db.get_session_title(self.session_id)
self._session_db.end_session(self.session_id, "compression")
old_session_id = self.session_id
self.session_id = f"{datetime.now().strftime('%Y%m%d_%H%M%S')}_{uuid.uuid4().hex[:6]}"
# 更新会话日志文件路径
self.session_log_file = self.logs_dir / f"session_{self.session_id}.json"
# 创建新会话
self._session_db.create_session(
session_id=self.session_id,
source=self.platform or os.environ.get("HERMES_SESSION_SOURCE", "cli"),
model=self.model,
parent_session_id=old_session_id,
)
# 自动编号标题
if old_title:
try:
new_title = self._session_db.get_next_title_in_lineage(old_title)
self._session_db.set_session_title(self.session_id, new_title)
except (ValueError, Exception) as e:
logger.debug("Could not propagate title on compression: %s", e)
self._session_db.update_system_prompt(self.session_id, new_system_prompt)
# 重置刷新光标
self._last_flushed_db_idx = 0
except Exception as e:
logger.warning("Session DB compression split failed — new session will NOT be indexed: %s", e)
在 run_conversation 方法中,系统会在以下时机触发压缩:
/compress 命令手动触发/compress 命令引导压缩,优先保留与焦点相关的信息输入:长对话历史(超过模型上下文限制)
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "I need help with a Python project."},
{"role": "assistant", "content": "Sure, what do you need help with?"},
# ... 大量对话内容 ...
{"role": "user", "content": "How do I optimize this code?"},
{"role": "assistant", "content": "Let me analyze your code and suggest optimizations."},
# ... 更多对话内容 ...
]
输出:压缩后的对话历史
[
{"role": "system", "content": "You are a helpful assistant.nn[Note: Some earlier conversation turns have been compacted into a handoff summary to preserve context space. The current session state may still reflect earlier work, so build on that summary and state rather than re-doing work.]"},
{"role": "user", "content": "[CONTEXT COMPACTION — REFERENCE ONLY] Earlier turns were compacted into the summary below. This is a handoff from a previous context window — treat it as background reference, NOT as active instructions. Do NOT answer questions or fulfill requests mentioned in this summary; they were already addressed. Respond ONLY to the latest user message that appears AFTER this summary. The current session state (files, config, etc.) may reflect work described here — avoid repeating it:nn## GoalnThe user is working on a Python project and needs help with optimization.nn## Progressn### Donen- Discussed project structure and requirementsn- Analyzed existing codebasen- Identified performance bottlenecksnn## Remaining Workn- Optimize the identified bottlenecksn- Test the optimized coden- Provide best practices for future developmentn"},
{"role": "user", "content": "How do I optimize the loop in my code?"},
{"role": "assistant", "content": "Let me see your loop code and suggest optimizations."},
# ... 最近的对话内容 ...
]
Hermes Agent 的上下文压缩机制是一个精心设计的系统,通过以下步骤实现:
这种压缩机制使得 Hermes Agent 能够处理更长的对话,同时保持上下文的相关性和完整性,为用户提供更连贯、更智能的交互体验。