2026-02-20 03:15:53 -08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
"""
|
|
|
|
|
Delegate Tool -- Subagent Architecture
|
|
|
|
|
|
|
|
|
|
Spawns child AIAgent instances with isolated context, restricted toolsets,
|
|
|
|
|
and their own terminal sessions. Supports single-task and batch (parallel)
|
|
|
|
|
modes. The parent blocks until all children complete.
|
|
|
|
|
|
|
|
|
|
Each child gets:
|
|
|
|
|
- A fresh conversation (no parent history)
|
|
|
|
|
- Its own task_id (own terminal session, file ops cache)
|
|
|
|
|
- A restricted toolset (configurable, with blocked tools always stripped)
|
|
|
|
|
- A focused system prompt built from the delegated goal + context
|
|
|
|
|
|
|
|
|
|
The parent's context only sees the delegation call and the summary result,
|
|
|
|
|
never the child's intermediate tool calls or reasoning.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import contextlib
|
|
|
|
|
import io
|
|
|
|
|
import json
|
|
|
|
|
import logging
|
2026-03-10 06:59:20 -07:00
|
|
|
logger = logging.getLogger(__name__)
|
2026-02-20 03:15:53 -08:00
|
|
|
import os
|
2026-02-24 04:13:32 -08:00
|
|
|
import sys
|
2026-02-20 03:15:53 -08:00
|
|
|
import time
|
|
|
|
|
from concurrent.futures import ThreadPoolExecutor, as_completed
|
|
|
|
|
from typing import Any, Dict, List, Optional
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Tools that children must never have access to
|
|
|
|
|
DELEGATE_BLOCKED_TOOLS = frozenset([
|
|
|
|
|
"delegate_task", # no recursive delegation
|
|
|
|
|
"clarify", # no user interaction
|
|
|
|
|
"memory", # no writes to shared MEMORY.md
|
|
|
|
|
"send_message", # no cross-platform side effects
|
|
|
|
|
"execute_code", # children should reason step-by-step, not write scripts
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
MAX_CONCURRENT_CHILDREN = 3
|
|
|
|
|
MAX_DEPTH = 2 # parent (0) -> child (1) -> grandchild rejected (2)
|
2026-03-02 00:51:01 -08:00
|
|
|
DEFAULT_MAX_ITERATIONS = 50
|
2026-02-20 03:15:53 -08:00
|
|
|
DEFAULT_TOOLSETS = ["terminal", "file", "web"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_delegate_requirements() -> bool:
|
|
|
|
|
"""Delegation has no external requirements -- always available."""
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _build_child_system_prompt(goal: str, context: Optional[str] = None) -> str:
|
|
|
|
|
"""Build a focused system prompt for a child agent."""
|
|
|
|
|
parts = [
|
|
|
|
|
"You are a focused subagent working on a specific delegated task.",
|
|
|
|
|
"",
|
|
|
|
|
f"YOUR TASK:\n{goal}",
|
|
|
|
|
]
|
|
|
|
|
if context and context.strip():
|
|
|
|
|
parts.append(f"\nCONTEXT:\n{context}")
|
|
|
|
|
parts.append(
|
|
|
|
|
"\nComplete this task using the tools available to you. "
|
|
|
|
|
"When finished, provide a clear, concise summary of:\n"
|
|
|
|
|
"- What you did\n"
|
|
|
|
|
"- What you found or accomplished\n"
|
|
|
|
|
"- Any files you created or modified\n"
|
|
|
|
|
"- Any issues encountered\n\n"
|
|
|
|
|
"Be thorough but concise -- your response is returned to the "
|
|
|
|
|
"parent agent as a summary."
|
|
|
|
|
)
|
|
|
|
|
return "\n".join(parts)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _strip_blocked_tools(toolsets: List[str]) -> List[str]:
|
|
|
|
|
"""Remove toolsets that contain only blocked tools."""
|
|
|
|
|
blocked_toolset_names = {
|
|
|
|
|
"delegation", "clarify", "memory", "code_execution",
|
|
|
|
|
}
|
|
|
|
|
return [t for t in toolsets if t not in blocked_toolset_names]
|
|
|
|
|
|
|
|
|
|
|
2026-02-28 23:29:49 -08:00
|
|
|
def _build_child_progress_callback(task_index: int, parent_agent, task_count: int = 1) -> Optional[callable]:
|
feat(gateway): expose subagent tool calls and thinking to user (fixes #169) (#186)
When subagents run via delegate_task, the user now sees real-time
progress instead of silence:
CLI: tree-view activity lines print above the delegation spinner
🔀 Delegating: research quantum computing
├─ 💭 "I'll search for papers first..."
├─ 🔍 web_search "quantum computing"
├─ 📖 read_file "paper.pdf"
└─ ⠹ working... (18.2s)
Gateway (Telegram/Discord): batched progress summaries sent every
5 tool calls to avoid message spam. Remaining tools flushed on
subagent completion.
Changes:
- agent/display.py: add KawaiiSpinner.print_above() to print
status lines above an active spinner without disrupting animation.
Uses captured stdout (self._out) so it works inside the child's
redirect_stdout(devnull).
- tools/delegate_tool.py: add _build_child_progress_callback()
that creates a per-child callback relaying tool calls and
thinking events to the parent's spinner (CLI) or progress
queue (gateway). Each child gets its own callback instance,
so parallel subagents don't share state. Includes _flush()
for gateway batch completion.
- run_agent.py: fire tool_progress_callback with '_thinking'
event when the model produces text content. Guarded by
_delegate_depth > 0 so only subagents fire this (prevents
gateway spam from main agent). REASONING_SCRATCHPAD/think/
reasoning XML tags are stripped before display.
Tests: 21 new tests covering print_above, callback builder,
thinking relay, SCRATCHPAD filtering, batching, flush, thread
isolation, delegate_depth guard, and prefix handling.
2026-03-01 10:18:00 +03:00
|
|
|
"""Build a callback that relays child agent tool calls to the parent display.
|
|
|
|
|
|
|
|
|
|
Two display paths:
|
|
|
|
|
CLI: prints tree-view lines above the parent's delegation spinner
|
|
|
|
|
Gateway: batches tool names and relays to parent's progress callback
|
|
|
|
|
|
|
|
|
|
Returns None if no display mechanism is available, in which case the
|
|
|
|
|
child agent runs with no progress callback (identical to current behavior).
|
|
|
|
|
"""
|
|
|
|
|
spinner = getattr(parent_agent, '_delegate_spinner', None)
|
|
|
|
|
parent_cb = getattr(parent_agent, 'tool_progress_callback', None)
|
|
|
|
|
|
|
|
|
|
if not spinner and not parent_cb:
|
|
|
|
|
return None # No display → no callback → zero behavior change
|
|
|
|
|
|
2026-02-28 23:29:49 -08:00
|
|
|
# Show 1-indexed prefix only in batch mode (multiple tasks)
|
|
|
|
|
prefix = f"[{task_index + 1}] " if task_count > 1 else ""
|
feat(gateway): expose subagent tool calls and thinking to user (fixes #169) (#186)
When subagents run via delegate_task, the user now sees real-time
progress instead of silence:
CLI: tree-view activity lines print above the delegation spinner
🔀 Delegating: research quantum computing
├─ 💭 "I'll search for papers first..."
├─ 🔍 web_search "quantum computing"
├─ 📖 read_file "paper.pdf"
└─ ⠹ working... (18.2s)
Gateway (Telegram/Discord): batched progress summaries sent every
5 tool calls to avoid message spam. Remaining tools flushed on
subagent completion.
Changes:
- agent/display.py: add KawaiiSpinner.print_above() to print
status lines above an active spinner without disrupting animation.
Uses captured stdout (self._out) so it works inside the child's
redirect_stdout(devnull).
- tools/delegate_tool.py: add _build_child_progress_callback()
that creates a per-child callback relaying tool calls and
thinking events to the parent's spinner (CLI) or progress
queue (gateway). Each child gets its own callback instance,
so parallel subagents don't share state. Includes _flush()
for gateway batch completion.
- run_agent.py: fire tool_progress_callback with '_thinking'
event when the model produces text content. Guarded by
_delegate_depth > 0 so only subagents fire this (prevents
gateway spam from main agent). REASONING_SCRATCHPAD/think/
reasoning XML tags are stripped before display.
Tests: 21 new tests covering print_above, callback builder,
thinking relay, SCRATCHPAD filtering, batching, flush, thread
isolation, delegate_depth guard, and prefix handling.
2026-03-01 10:18:00 +03:00
|
|
|
|
|
|
|
|
# Gateway: batch tool names, flush periodically
|
|
|
|
|
_BATCH_SIZE = 5
|
|
|
|
|
_batch: List[str] = []
|
|
|
|
|
|
|
|
|
|
def _callback(tool_name: str, preview: str = None):
|
|
|
|
|
# Special "_thinking" event: model produced text content (reasoning)
|
|
|
|
|
if tool_name == "_thinking":
|
|
|
|
|
if spinner:
|
|
|
|
|
short = (preview[:55] + "...") if preview and len(preview) > 55 else (preview or "")
|
|
|
|
|
try:
|
|
|
|
|
spinner.print_above(f" {prefix}├─ 💭 \"{short}\"")
|
2026-03-10 06:59:20 -07:00
|
|
|
except Exception as e:
|
|
|
|
|
logger.debug("Spinner print_above failed: %s", e)
|
feat(gateway): expose subagent tool calls and thinking to user (fixes #169) (#186)
When subagents run via delegate_task, the user now sees real-time
progress instead of silence:
CLI: tree-view activity lines print above the delegation spinner
🔀 Delegating: research quantum computing
├─ 💭 "I'll search for papers first..."
├─ 🔍 web_search "quantum computing"
├─ 📖 read_file "paper.pdf"
└─ ⠹ working... (18.2s)
Gateway (Telegram/Discord): batched progress summaries sent every
5 tool calls to avoid message spam. Remaining tools flushed on
subagent completion.
Changes:
- agent/display.py: add KawaiiSpinner.print_above() to print
status lines above an active spinner without disrupting animation.
Uses captured stdout (self._out) so it works inside the child's
redirect_stdout(devnull).
- tools/delegate_tool.py: add _build_child_progress_callback()
that creates a per-child callback relaying tool calls and
thinking events to the parent's spinner (CLI) or progress
queue (gateway). Each child gets its own callback instance,
so parallel subagents don't share state. Includes _flush()
for gateway batch completion.
- run_agent.py: fire tool_progress_callback with '_thinking'
event when the model produces text content. Guarded by
_delegate_depth > 0 so only subagents fire this (prevents
gateway spam from main agent). REASONING_SCRATCHPAD/think/
reasoning XML tags are stripped before display.
Tests: 21 new tests covering print_above, callback builder,
thinking relay, SCRATCHPAD filtering, batching, flush, thread
isolation, delegate_depth guard, and prefix handling.
2026-03-01 10:18:00 +03:00
|
|
|
# Don't relay thinking to gateway (too noisy for chat)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# Regular tool call event
|
|
|
|
|
if spinner:
|
|
|
|
|
short = (preview[:35] + "...") if preview and len(preview) > 35 else (preview or "")
|
|
|
|
|
tool_emojis = {
|
|
|
|
|
"terminal": "💻", "web_search": "🔍", "web_extract": "📄",
|
|
|
|
|
"read_file": "📖", "write_file": "✍️", "patch": "🔧",
|
|
|
|
|
"search_files": "🔎", "list_directory": "📂",
|
|
|
|
|
"browser_navigate": "🌐", "browser_click": "👆",
|
|
|
|
|
"text_to_speech": "🔊", "image_generate": "🎨",
|
|
|
|
|
"vision_analyze": "👁️", "process": "⚙️",
|
|
|
|
|
}
|
|
|
|
|
emoji = tool_emojis.get(tool_name, "⚡")
|
|
|
|
|
line = f" {prefix}├─ {emoji} {tool_name}"
|
|
|
|
|
if short:
|
|
|
|
|
line += f" \"{short}\""
|
|
|
|
|
try:
|
|
|
|
|
spinner.print_above(line)
|
2026-03-10 06:59:20 -07:00
|
|
|
except Exception as e:
|
|
|
|
|
logger.debug("Spinner print_above failed: %s", e)
|
feat(gateway): expose subagent tool calls and thinking to user (fixes #169) (#186)
When subagents run via delegate_task, the user now sees real-time
progress instead of silence:
CLI: tree-view activity lines print above the delegation spinner
🔀 Delegating: research quantum computing
├─ 💭 "I'll search for papers first..."
├─ 🔍 web_search "quantum computing"
├─ 📖 read_file "paper.pdf"
└─ ⠹ working... (18.2s)
Gateway (Telegram/Discord): batched progress summaries sent every
5 tool calls to avoid message spam. Remaining tools flushed on
subagent completion.
Changes:
- agent/display.py: add KawaiiSpinner.print_above() to print
status lines above an active spinner without disrupting animation.
Uses captured stdout (self._out) so it works inside the child's
redirect_stdout(devnull).
- tools/delegate_tool.py: add _build_child_progress_callback()
that creates a per-child callback relaying tool calls and
thinking events to the parent's spinner (CLI) or progress
queue (gateway). Each child gets its own callback instance,
so parallel subagents don't share state. Includes _flush()
for gateway batch completion.
- run_agent.py: fire tool_progress_callback with '_thinking'
event when the model produces text content. Guarded by
_delegate_depth > 0 so only subagents fire this (prevents
gateway spam from main agent). REASONING_SCRATCHPAD/think/
reasoning XML tags are stripped before display.
Tests: 21 new tests covering print_above, callback builder,
thinking relay, SCRATCHPAD filtering, batching, flush, thread
isolation, delegate_depth guard, and prefix handling.
2026-03-01 10:18:00 +03:00
|
|
|
|
|
|
|
|
if parent_cb:
|
|
|
|
|
_batch.append(tool_name)
|
|
|
|
|
if len(_batch) >= _BATCH_SIZE:
|
|
|
|
|
summary = ", ".join(_batch)
|
|
|
|
|
try:
|
|
|
|
|
parent_cb("subagent_progress", f"🔀 {prefix}{summary}")
|
2026-03-10 06:59:20 -07:00
|
|
|
except Exception as e:
|
|
|
|
|
logger.debug("Parent callback failed: %s", e)
|
feat(gateway): expose subagent tool calls and thinking to user (fixes #169) (#186)
When subagents run via delegate_task, the user now sees real-time
progress instead of silence:
CLI: tree-view activity lines print above the delegation spinner
🔀 Delegating: research quantum computing
├─ 💭 "I'll search for papers first..."
├─ 🔍 web_search "quantum computing"
├─ 📖 read_file "paper.pdf"
└─ ⠹ working... (18.2s)
Gateway (Telegram/Discord): batched progress summaries sent every
5 tool calls to avoid message spam. Remaining tools flushed on
subagent completion.
Changes:
- agent/display.py: add KawaiiSpinner.print_above() to print
status lines above an active spinner without disrupting animation.
Uses captured stdout (self._out) so it works inside the child's
redirect_stdout(devnull).
- tools/delegate_tool.py: add _build_child_progress_callback()
that creates a per-child callback relaying tool calls and
thinking events to the parent's spinner (CLI) or progress
queue (gateway). Each child gets its own callback instance,
so parallel subagents don't share state. Includes _flush()
for gateway batch completion.
- run_agent.py: fire tool_progress_callback with '_thinking'
event when the model produces text content. Guarded by
_delegate_depth > 0 so only subagents fire this (prevents
gateway spam from main agent). REASONING_SCRATCHPAD/think/
reasoning XML tags are stripped before display.
Tests: 21 new tests covering print_above, callback builder,
thinking relay, SCRATCHPAD filtering, batching, flush, thread
isolation, delegate_depth guard, and prefix handling.
2026-03-01 10:18:00 +03:00
|
|
|
_batch.clear()
|
|
|
|
|
|
|
|
|
|
def _flush():
|
|
|
|
|
"""Flush remaining batched tool names to gateway on completion."""
|
|
|
|
|
if parent_cb and _batch:
|
|
|
|
|
summary = ", ".join(_batch)
|
|
|
|
|
try:
|
|
|
|
|
parent_cb("subagent_progress", f"🔀 {prefix}{summary}")
|
2026-03-10 06:59:20 -07:00
|
|
|
except Exception as e:
|
|
|
|
|
logger.debug("Parent callback flush failed: %s", e)
|
feat(gateway): expose subagent tool calls and thinking to user (fixes #169) (#186)
When subagents run via delegate_task, the user now sees real-time
progress instead of silence:
CLI: tree-view activity lines print above the delegation spinner
🔀 Delegating: research quantum computing
├─ 💭 "I'll search for papers first..."
├─ 🔍 web_search "quantum computing"
├─ 📖 read_file "paper.pdf"
└─ ⠹ working... (18.2s)
Gateway (Telegram/Discord): batched progress summaries sent every
5 tool calls to avoid message spam. Remaining tools flushed on
subagent completion.
Changes:
- agent/display.py: add KawaiiSpinner.print_above() to print
status lines above an active spinner without disrupting animation.
Uses captured stdout (self._out) so it works inside the child's
redirect_stdout(devnull).
- tools/delegate_tool.py: add _build_child_progress_callback()
that creates a per-child callback relaying tool calls and
thinking events to the parent's spinner (CLI) or progress
queue (gateway). Each child gets its own callback instance,
so parallel subagents don't share state. Includes _flush()
for gateway batch completion.
- run_agent.py: fire tool_progress_callback with '_thinking'
event when the model produces text content. Guarded by
_delegate_depth > 0 so only subagents fire this (prevents
gateway spam from main agent). REASONING_SCRATCHPAD/think/
reasoning XML tags are stripped before display.
Tests: 21 new tests covering print_above, callback builder,
thinking relay, SCRATCHPAD filtering, batching, flush, thread
isolation, delegate_depth guard, and prefix handling.
2026-03-01 10:18:00 +03:00
|
|
|
_batch.clear()
|
|
|
|
|
|
|
|
|
|
_callback._flush = _flush
|
|
|
|
|
return _callback
|
|
|
|
|
|
|
|
|
|
|
2026-02-20 03:15:53 -08:00
|
|
|
def _run_single_child(
|
|
|
|
|
task_index: int,
|
|
|
|
|
goal: str,
|
|
|
|
|
context: Optional[str],
|
|
|
|
|
toolsets: Optional[List[str]],
|
|
|
|
|
model: Optional[str],
|
|
|
|
|
max_iterations: int,
|
|
|
|
|
parent_agent,
|
2026-02-28 23:29:49 -08:00
|
|
|
task_count: int = 1,
|
feat: configurable subagent provider:model with full credential resolution
Adds delegation.model and delegation.provider config fields so subagents
can run on a completely different provider:model pair than the parent agent.
When delegation.provider is set, the system resolves the full credential
bundle (base_url, api_key, api_mode) via resolve_runtime_provider() —
the same path used by CLI/gateway startup. This means all configured
providers work out of the box: openrouter, nous, zai, kimi-coding,
minimax, minimax-cn.
Key design decisions:
- Provider resolution uses hermes_cli.runtime_provider (single source of
truth for credential resolution across CLI, gateway, cron, and now
delegation)
- When only delegation.model is set (no provider), the model name changes
but parent credentials are inherited (for switching models within the
same provider like OpenRouter)
- When delegation.provider is set, full credentials are resolved
independently — enabling cross-provider delegation (e.g. parent on
Nous Portal, subagents on OpenRouter)
- Clear error messages if provider resolution fails (missing API key,
unknown provider name)
- _load_config() now falls back to hermes_cli.config.load_config() for
gateway/cron contexts where CLI_CONFIG is unavailable
Based on PR #791 by 0xbyt4 (closes #609), reworked to use proper
provider credential resolution instead of passing provider as metadata.
Co-authored-by: 0xbyt4 <0xbyt4@users.noreply.github.com>
2026-03-11 06:12:21 -07:00
|
|
|
# Credential overrides from delegation config (provider:model resolution)
|
|
|
|
|
override_provider: Optional[str] = None,
|
|
|
|
|
override_base_url: Optional[str] = None,
|
|
|
|
|
override_api_key: Optional[str] = None,
|
|
|
|
|
override_api_mode: Optional[str] = None,
|
2026-02-20 03:15:53 -08:00
|
|
|
) -> Dict[str, Any]:
|
|
|
|
|
"""
|
|
|
|
|
Spawn and run a single child agent. Called from within a thread.
|
|
|
|
|
Returns a structured result dict.
|
feat: configurable subagent provider:model with full credential resolution
Adds delegation.model and delegation.provider config fields so subagents
can run on a completely different provider:model pair than the parent agent.
When delegation.provider is set, the system resolves the full credential
bundle (base_url, api_key, api_mode) via resolve_runtime_provider() —
the same path used by CLI/gateway startup. This means all configured
providers work out of the box: openrouter, nous, zai, kimi-coding,
minimax, minimax-cn.
Key design decisions:
- Provider resolution uses hermes_cli.runtime_provider (single source of
truth for credential resolution across CLI, gateway, cron, and now
delegation)
- When only delegation.model is set (no provider), the model name changes
but parent credentials are inherited (for switching models within the
same provider like OpenRouter)
- When delegation.provider is set, full credentials are resolved
independently — enabling cross-provider delegation (e.g. parent on
Nous Portal, subagents on OpenRouter)
- Clear error messages if provider resolution fails (missing API key,
unknown provider name)
- _load_config() now falls back to hermes_cli.config.load_config() for
gateway/cron contexts where CLI_CONFIG is unavailable
Based on PR #791 by 0xbyt4 (closes #609), reworked to use proper
provider credential resolution instead of passing provider as metadata.
Co-authored-by: 0xbyt4 <0xbyt4@users.noreply.github.com>
2026-03-11 06:12:21 -07:00
|
|
|
|
|
|
|
|
When override_* params are set (from delegation config), the child uses
|
|
|
|
|
those credentials instead of inheriting from the parent. This enables
|
|
|
|
|
routing subagents to a different provider:model pair (e.g. cheap/fast
|
|
|
|
|
model on OpenRouter while the parent runs on Nous Portal).
|
2026-02-20 03:15:53 -08:00
|
|
|
"""
|
|
|
|
|
from run_agent import AIAgent
|
|
|
|
|
|
|
|
|
|
child_start = time.monotonic()
|
|
|
|
|
|
2026-03-06 17:36:06 -08:00
|
|
|
# When no explicit toolsets given, inherit from parent's enabled toolsets
|
|
|
|
|
# so disabled tools (e.g. web) don't leak to subagents.
|
|
|
|
|
if toolsets:
|
|
|
|
|
child_toolsets = _strip_blocked_tools(toolsets)
|
|
|
|
|
elif parent_agent and getattr(parent_agent, "enabled_toolsets", None):
|
|
|
|
|
child_toolsets = _strip_blocked_tools(parent_agent.enabled_toolsets)
|
|
|
|
|
else:
|
|
|
|
|
child_toolsets = _strip_blocked_tools(DEFAULT_TOOLSETS)
|
2026-02-20 03:15:53 -08:00
|
|
|
|
|
|
|
|
child_prompt = _build_child_system_prompt(goal, context)
|
|
|
|
|
|
|
|
|
|
try:
|
2026-02-26 10:56:29 -08:00
|
|
|
# Extract parent's API key so subagents inherit auth (e.g. Nous Portal).
|
|
|
|
|
parent_api_key = getattr(parent_agent, "api_key", None)
|
|
|
|
|
if (not parent_api_key) and hasattr(parent_agent, "_client_kwargs"):
|
2026-02-25 22:37:36 -05:00
|
|
|
parent_api_key = parent_agent._client_kwargs.get("api_key")
|
|
|
|
|
|
feat(gateway): expose subagent tool calls and thinking to user (fixes #169) (#186)
When subagents run via delegate_task, the user now sees real-time
progress instead of silence:
CLI: tree-view activity lines print above the delegation spinner
🔀 Delegating: research quantum computing
├─ 💭 "I'll search for papers first..."
├─ 🔍 web_search "quantum computing"
├─ 📖 read_file "paper.pdf"
└─ ⠹ working... (18.2s)
Gateway (Telegram/Discord): batched progress summaries sent every
5 tool calls to avoid message spam. Remaining tools flushed on
subagent completion.
Changes:
- agent/display.py: add KawaiiSpinner.print_above() to print
status lines above an active spinner without disrupting animation.
Uses captured stdout (self._out) so it works inside the child's
redirect_stdout(devnull).
- tools/delegate_tool.py: add _build_child_progress_callback()
that creates a per-child callback relaying tool calls and
thinking events to the parent's spinner (CLI) or progress
queue (gateway). Each child gets its own callback instance,
so parallel subagents don't share state. Includes _flush()
for gateway batch completion.
- run_agent.py: fire tool_progress_callback with '_thinking'
event when the model produces text content. Guarded by
_delegate_depth > 0 so only subagents fire this (prevents
gateway spam from main agent). REASONING_SCRATCHPAD/think/
reasoning XML tags are stripped before display.
Tests: 21 new tests covering print_above, callback builder,
thinking relay, SCRATCHPAD filtering, batching, flush, thread
isolation, delegate_depth guard, and prefix handling.
2026-03-01 10:18:00 +03:00
|
|
|
# Build progress callback to relay tool calls to parent display
|
2026-02-28 23:29:49 -08:00
|
|
|
child_progress_cb = _build_child_progress_callback(task_index, parent_agent, task_count)
|
feat(gateway): expose subagent tool calls and thinking to user (fixes #169) (#186)
When subagents run via delegate_task, the user now sees real-time
progress instead of silence:
CLI: tree-view activity lines print above the delegation spinner
🔀 Delegating: research quantum computing
├─ 💭 "I'll search for papers first..."
├─ 🔍 web_search "quantum computing"
├─ 📖 read_file "paper.pdf"
└─ ⠹ working... (18.2s)
Gateway (Telegram/Discord): batched progress summaries sent every
5 tool calls to avoid message spam. Remaining tools flushed on
subagent completion.
Changes:
- agent/display.py: add KawaiiSpinner.print_above() to print
status lines above an active spinner without disrupting animation.
Uses captured stdout (self._out) so it works inside the child's
redirect_stdout(devnull).
- tools/delegate_tool.py: add _build_child_progress_callback()
that creates a per-child callback relaying tool calls and
thinking events to the parent's spinner (CLI) or progress
queue (gateway). Each child gets its own callback instance,
so parallel subagents don't share state. Includes _flush()
for gateway batch completion.
- run_agent.py: fire tool_progress_callback with '_thinking'
event when the model produces text content. Guarded by
_delegate_depth > 0 so only subagents fire this (prevents
gateway spam from main agent). REASONING_SCRATCHPAD/think/
reasoning XML tags are stripped before display.
Tests: 21 new tests covering print_above, callback builder,
thinking relay, SCRATCHPAD filtering, batching, flush, thread
isolation, delegate_depth guard, and prefix handling.
2026-03-01 10:18:00 +03:00
|
|
|
|
2026-03-07 08:16:37 -08:00
|
|
|
# Share the parent's iteration budget so subagent tool calls
|
|
|
|
|
# count toward the session-wide limit.
|
|
|
|
|
shared_budget = getattr(parent_agent, "iteration_budget", None)
|
|
|
|
|
|
feat: configurable subagent provider:model with full credential resolution
Adds delegation.model and delegation.provider config fields so subagents
can run on a completely different provider:model pair than the parent agent.
When delegation.provider is set, the system resolves the full credential
bundle (base_url, api_key, api_mode) via resolve_runtime_provider() —
the same path used by CLI/gateway startup. This means all configured
providers work out of the box: openrouter, nous, zai, kimi-coding,
minimax, minimax-cn.
Key design decisions:
- Provider resolution uses hermes_cli.runtime_provider (single source of
truth for credential resolution across CLI, gateway, cron, and now
delegation)
- When only delegation.model is set (no provider), the model name changes
but parent credentials are inherited (for switching models within the
same provider like OpenRouter)
- When delegation.provider is set, full credentials are resolved
independently — enabling cross-provider delegation (e.g. parent on
Nous Portal, subagents on OpenRouter)
- Clear error messages if provider resolution fails (missing API key,
unknown provider name)
- _load_config() now falls back to hermes_cli.config.load_config() for
gateway/cron contexts where CLI_CONFIG is unavailable
Based on PR #791 by 0xbyt4 (closes #609), reworked to use proper
provider credential resolution instead of passing provider as metadata.
Co-authored-by: 0xbyt4 <0xbyt4@users.noreply.github.com>
2026-03-11 06:12:21 -07:00
|
|
|
# Resolve effective credentials: config override > parent inherit
|
|
|
|
|
effective_model = model or parent_agent.model
|
|
|
|
|
effective_provider = override_provider or getattr(parent_agent, "provider", None)
|
|
|
|
|
effective_base_url = override_base_url or parent_agent.base_url
|
|
|
|
|
effective_api_key = override_api_key or parent_api_key
|
|
|
|
|
effective_api_mode = override_api_mode or getattr(parent_agent, "api_mode", None)
|
|
|
|
|
|
2026-02-20 03:15:53 -08:00
|
|
|
child = AIAgent(
|
feat: configurable subagent provider:model with full credential resolution
Adds delegation.model and delegation.provider config fields so subagents
can run on a completely different provider:model pair than the parent agent.
When delegation.provider is set, the system resolves the full credential
bundle (base_url, api_key, api_mode) via resolve_runtime_provider() —
the same path used by CLI/gateway startup. This means all configured
providers work out of the box: openrouter, nous, zai, kimi-coding,
minimax, minimax-cn.
Key design decisions:
- Provider resolution uses hermes_cli.runtime_provider (single source of
truth for credential resolution across CLI, gateway, cron, and now
delegation)
- When only delegation.model is set (no provider), the model name changes
but parent credentials are inherited (for switching models within the
same provider like OpenRouter)
- When delegation.provider is set, full credentials are resolved
independently — enabling cross-provider delegation (e.g. parent on
Nous Portal, subagents on OpenRouter)
- Clear error messages if provider resolution fails (missing API key,
unknown provider name)
- _load_config() now falls back to hermes_cli.config.load_config() for
gateway/cron contexts where CLI_CONFIG is unavailable
Based on PR #791 by 0xbyt4 (closes #609), reworked to use proper
provider credential resolution instead of passing provider as metadata.
Co-authored-by: 0xbyt4 <0xbyt4@users.noreply.github.com>
2026-03-11 06:12:21 -07:00
|
|
|
base_url=effective_base_url,
|
|
|
|
|
api_key=effective_api_key,
|
|
|
|
|
model=effective_model,
|
|
|
|
|
provider=effective_provider,
|
|
|
|
|
api_mode=effective_api_mode,
|
2026-02-20 03:15:53 -08:00
|
|
|
max_iterations=max_iterations,
|
2026-03-07 11:29:17 -08:00
|
|
|
max_tokens=getattr(parent_agent, "max_tokens", None),
|
|
|
|
|
reasoning_config=getattr(parent_agent, "reasoning_config", None),
|
|
|
|
|
prefill_messages=getattr(parent_agent, "prefill_messages", None),
|
2026-02-20 03:15:53 -08:00
|
|
|
enabled_toolsets=child_toolsets,
|
|
|
|
|
quiet_mode=True,
|
|
|
|
|
ephemeral_system_prompt=child_prompt,
|
|
|
|
|
log_prefix=f"[subagent-{task_index}]",
|
|
|
|
|
platform=parent_agent.platform,
|
|
|
|
|
skip_context_files=True,
|
|
|
|
|
skip_memory=True,
|
|
|
|
|
clarify_callback=None,
|
|
|
|
|
session_db=getattr(parent_agent, '_session_db', None),
|
|
|
|
|
providers_allowed=parent_agent.providers_allowed,
|
|
|
|
|
providers_ignored=parent_agent.providers_ignored,
|
|
|
|
|
providers_order=parent_agent.providers_order,
|
|
|
|
|
provider_sort=parent_agent.provider_sort,
|
feat(gateway): expose subagent tool calls and thinking to user (fixes #169) (#186)
When subagents run via delegate_task, the user now sees real-time
progress instead of silence:
CLI: tree-view activity lines print above the delegation spinner
🔀 Delegating: research quantum computing
├─ 💭 "I'll search for papers first..."
├─ 🔍 web_search "quantum computing"
├─ 📖 read_file "paper.pdf"
└─ ⠹ working... (18.2s)
Gateway (Telegram/Discord): batched progress summaries sent every
5 tool calls to avoid message spam. Remaining tools flushed on
subagent completion.
Changes:
- agent/display.py: add KawaiiSpinner.print_above() to print
status lines above an active spinner without disrupting animation.
Uses captured stdout (self._out) so it works inside the child's
redirect_stdout(devnull).
- tools/delegate_tool.py: add _build_child_progress_callback()
that creates a per-child callback relaying tool calls and
thinking events to the parent's spinner (CLI) or progress
queue (gateway). Each child gets its own callback instance,
so parallel subagents don't share state. Includes _flush()
for gateway batch completion.
- run_agent.py: fire tool_progress_callback with '_thinking'
event when the model produces text content. Guarded by
_delegate_depth > 0 so only subagents fire this (prevents
gateway spam from main agent). REASONING_SCRATCHPAD/think/
reasoning XML tags are stripped before display.
Tests: 21 new tests covering print_above, callback builder,
thinking relay, SCRATCHPAD filtering, batching, flush, thread
isolation, delegate_depth guard, and prefix handling.
2026-03-01 10:18:00 +03:00
|
|
|
tool_progress_callback=child_progress_cb,
|
2026-03-07 08:16:37 -08:00
|
|
|
iteration_budget=shared_budget,
|
2026-02-20 03:15:53 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Set delegation depth so children can't spawn grandchildren
|
|
|
|
|
child._delegate_depth = getattr(parent_agent, '_delegate_depth', 0) + 1
|
|
|
|
|
|
|
|
|
|
# Register child for interrupt propagation
|
|
|
|
|
if hasattr(parent_agent, '_active_children'):
|
|
|
|
|
parent_agent._active_children.append(child)
|
|
|
|
|
|
|
|
|
|
# Run with stdout/stderr suppressed to prevent interleaved output
|
|
|
|
|
devnull = io.StringIO()
|
|
|
|
|
with contextlib.redirect_stdout(devnull), contextlib.redirect_stderr(devnull):
|
|
|
|
|
result = child.run_conversation(user_message=goal)
|
|
|
|
|
|
feat(gateway): expose subagent tool calls and thinking to user (fixes #169) (#186)
When subagents run via delegate_task, the user now sees real-time
progress instead of silence:
CLI: tree-view activity lines print above the delegation spinner
🔀 Delegating: research quantum computing
├─ 💭 "I'll search for papers first..."
├─ 🔍 web_search "quantum computing"
├─ 📖 read_file "paper.pdf"
└─ ⠹ working... (18.2s)
Gateway (Telegram/Discord): batched progress summaries sent every
5 tool calls to avoid message spam. Remaining tools flushed on
subagent completion.
Changes:
- agent/display.py: add KawaiiSpinner.print_above() to print
status lines above an active spinner without disrupting animation.
Uses captured stdout (self._out) so it works inside the child's
redirect_stdout(devnull).
- tools/delegate_tool.py: add _build_child_progress_callback()
that creates a per-child callback relaying tool calls and
thinking events to the parent's spinner (CLI) or progress
queue (gateway). Each child gets its own callback instance,
so parallel subagents don't share state. Includes _flush()
for gateway batch completion.
- run_agent.py: fire tool_progress_callback with '_thinking'
event when the model produces text content. Guarded by
_delegate_depth > 0 so only subagents fire this (prevents
gateway spam from main agent). REASONING_SCRATCHPAD/think/
reasoning XML tags are stripped before display.
Tests: 21 new tests covering print_above, callback builder,
thinking relay, SCRATCHPAD filtering, batching, flush, thread
isolation, delegate_depth guard, and prefix handling.
2026-03-01 10:18:00 +03:00
|
|
|
# Flush any remaining batched progress to gateway
|
|
|
|
|
if child_progress_cb and hasattr(child_progress_cb, '_flush'):
|
|
|
|
|
try:
|
|
|
|
|
child_progress_cb._flush()
|
2026-03-10 06:59:20 -07:00
|
|
|
except Exception as e:
|
|
|
|
|
logger.debug("Progress callback flush failed: %s", e)
|
feat(gateway): expose subagent tool calls and thinking to user (fixes #169) (#186)
When subagents run via delegate_task, the user now sees real-time
progress instead of silence:
CLI: tree-view activity lines print above the delegation spinner
🔀 Delegating: research quantum computing
├─ 💭 "I'll search for papers first..."
├─ 🔍 web_search "quantum computing"
├─ 📖 read_file "paper.pdf"
└─ ⠹ working... (18.2s)
Gateway (Telegram/Discord): batched progress summaries sent every
5 tool calls to avoid message spam. Remaining tools flushed on
subagent completion.
Changes:
- agent/display.py: add KawaiiSpinner.print_above() to print
status lines above an active spinner without disrupting animation.
Uses captured stdout (self._out) so it works inside the child's
redirect_stdout(devnull).
- tools/delegate_tool.py: add _build_child_progress_callback()
that creates a per-child callback relaying tool calls and
thinking events to the parent's spinner (CLI) or progress
queue (gateway). Each child gets its own callback instance,
so parallel subagents don't share state. Includes _flush()
for gateway batch completion.
- run_agent.py: fire tool_progress_callback with '_thinking'
event when the model produces text content. Guarded by
_delegate_depth > 0 so only subagents fire this (prevents
gateway spam from main agent). REASONING_SCRATCHPAD/think/
reasoning XML tags are stripped before display.
Tests: 21 new tests covering print_above, callback builder,
thinking relay, SCRATCHPAD filtering, batching, flush, thread
isolation, delegate_depth guard, and prefix handling.
2026-03-01 10:18:00 +03:00
|
|
|
|
2026-02-20 03:15:53 -08:00
|
|
|
duration = round(time.monotonic() - child_start, 2)
|
|
|
|
|
|
|
|
|
|
summary = result.get("final_response") or ""
|
|
|
|
|
completed = result.get("completed", False)
|
|
|
|
|
interrupted = result.get("interrupted", False)
|
|
|
|
|
api_calls = result.get("api_calls", 0)
|
|
|
|
|
|
|
|
|
|
if interrupted:
|
|
|
|
|
status = "interrupted"
|
|
|
|
|
elif completed and summary:
|
|
|
|
|
status = "completed"
|
|
|
|
|
else:
|
|
|
|
|
status = "failed"
|
|
|
|
|
|
|
|
|
|
entry: Dict[str, Any] = {
|
|
|
|
|
"task_index": task_index,
|
|
|
|
|
"status": status,
|
|
|
|
|
"summary": summary,
|
|
|
|
|
"api_calls": api_calls,
|
|
|
|
|
"duration_seconds": duration,
|
|
|
|
|
}
|
|
|
|
|
if status == "failed":
|
|
|
|
|
entry["error"] = result.get("error", "Subagent did not produce a response.")
|
|
|
|
|
|
|
|
|
|
return entry
|
|
|
|
|
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
duration = round(time.monotonic() - child_start, 2)
|
|
|
|
|
logging.exception(f"[subagent-{task_index}] failed")
|
|
|
|
|
return {
|
|
|
|
|
"task_index": task_index,
|
|
|
|
|
"status": "error",
|
|
|
|
|
"summary": None,
|
|
|
|
|
"error": str(exc),
|
|
|
|
|
"api_calls": 0,
|
|
|
|
|
"duration_seconds": duration,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
# Unregister child from interrupt propagation
|
|
|
|
|
if hasattr(parent_agent, '_active_children'):
|
|
|
|
|
try:
|
|
|
|
|
parent_agent._active_children.remove(child)
|
2026-03-10 06:59:20 -07:00
|
|
|
except (ValueError, UnboundLocalError) as e:
|
|
|
|
|
logger.debug("Could not remove child from active_children: %s", e)
|
2026-02-20 03:15:53 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def delegate_task(
|
|
|
|
|
goal: Optional[str] = None,
|
|
|
|
|
context: Optional[str] = None,
|
|
|
|
|
toolsets: Optional[List[str]] = None,
|
|
|
|
|
tasks: Optional[List[Dict[str, Any]]] = None,
|
|
|
|
|
max_iterations: Optional[int] = None,
|
|
|
|
|
parent_agent=None,
|
|
|
|
|
) -> str:
|
|
|
|
|
"""
|
|
|
|
|
Spawn one or more child agents to handle delegated tasks.
|
|
|
|
|
|
|
|
|
|
Supports two modes:
|
|
|
|
|
- Single: provide goal (+ optional context, toolsets)
|
|
|
|
|
- Batch: provide tasks array [{goal, context, toolsets}, ...]
|
|
|
|
|
|
|
|
|
|
Returns JSON with results array, one entry per task.
|
|
|
|
|
"""
|
|
|
|
|
if parent_agent is None:
|
|
|
|
|
return json.dumps({"error": "delegate_task requires a parent agent context."})
|
|
|
|
|
|
|
|
|
|
# Depth limit
|
|
|
|
|
depth = getattr(parent_agent, '_delegate_depth', 0)
|
|
|
|
|
if depth >= MAX_DEPTH:
|
|
|
|
|
return json.dumps({
|
|
|
|
|
"error": (
|
|
|
|
|
f"Delegation depth limit reached ({MAX_DEPTH}). "
|
|
|
|
|
"Subagents cannot spawn further subagents."
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
# Load config
|
|
|
|
|
cfg = _load_config()
|
|
|
|
|
default_max_iter = cfg.get("max_iterations", DEFAULT_MAX_ITERATIONS)
|
|
|
|
|
effective_max_iter = max_iterations or default_max_iter
|
|
|
|
|
|
feat: configurable subagent provider:model with full credential resolution
Adds delegation.model and delegation.provider config fields so subagents
can run on a completely different provider:model pair than the parent agent.
When delegation.provider is set, the system resolves the full credential
bundle (base_url, api_key, api_mode) via resolve_runtime_provider() —
the same path used by CLI/gateway startup. This means all configured
providers work out of the box: openrouter, nous, zai, kimi-coding,
minimax, minimax-cn.
Key design decisions:
- Provider resolution uses hermes_cli.runtime_provider (single source of
truth for credential resolution across CLI, gateway, cron, and now
delegation)
- When only delegation.model is set (no provider), the model name changes
but parent credentials are inherited (for switching models within the
same provider like OpenRouter)
- When delegation.provider is set, full credentials are resolved
independently — enabling cross-provider delegation (e.g. parent on
Nous Portal, subagents on OpenRouter)
- Clear error messages if provider resolution fails (missing API key,
unknown provider name)
- _load_config() now falls back to hermes_cli.config.load_config() for
gateway/cron contexts where CLI_CONFIG is unavailable
Based on PR #791 by 0xbyt4 (closes #609), reworked to use proper
provider credential resolution instead of passing provider as metadata.
Co-authored-by: 0xbyt4 <0xbyt4@users.noreply.github.com>
2026-03-11 06:12:21 -07:00
|
|
|
# Resolve delegation credentials (provider:model pair).
|
|
|
|
|
# When delegation.provider is configured, this resolves the full credential
|
|
|
|
|
# bundle (base_url, api_key, api_mode) via the same runtime provider system
|
|
|
|
|
# used by CLI/gateway startup. When unconfigured, returns None values so
|
|
|
|
|
# children inherit from the parent.
|
|
|
|
|
try:
|
|
|
|
|
creds = _resolve_delegation_credentials(cfg, parent_agent)
|
|
|
|
|
except ValueError as exc:
|
|
|
|
|
return json.dumps({"error": str(exc)})
|
|
|
|
|
|
2026-02-20 03:15:53 -08:00
|
|
|
# Normalize to task list
|
|
|
|
|
if tasks and isinstance(tasks, list):
|
|
|
|
|
task_list = tasks[:MAX_CONCURRENT_CHILDREN]
|
|
|
|
|
elif goal and isinstance(goal, str) and goal.strip():
|
|
|
|
|
task_list = [{"goal": goal, "context": context, "toolsets": toolsets}]
|
|
|
|
|
else:
|
|
|
|
|
return json.dumps({"error": "Provide either 'goal' (single task) or 'tasks' (batch)."})
|
|
|
|
|
|
|
|
|
|
if not task_list:
|
|
|
|
|
return json.dumps({"error": "No tasks provided."})
|
|
|
|
|
|
|
|
|
|
# Validate each task has a goal
|
|
|
|
|
for i, task in enumerate(task_list):
|
|
|
|
|
if not task.get("goal", "").strip():
|
|
|
|
|
return json.dumps({"error": f"Task {i} is missing a 'goal'."})
|
|
|
|
|
|
|
|
|
|
overall_start = time.monotonic()
|
|
|
|
|
results = []
|
|
|
|
|
|
2026-02-20 03:23:23 -08:00
|
|
|
n_tasks = len(task_list)
|
|
|
|
|
# Track goal labels for progress display (truncated for readability)
|
|
|
|
|
task_labels = [t["goal"][:40] for t in task_list]
|
|
|
|
|
|
|
|
|
|
if n_tasks == 1:
|
2026-02-20 03:15:53 -08:00
|
|
|
# Single task -- run directly (no thread pool overhead)
|
|
|
|
|
t = task_list[0]
|
|
|
|
|
result = _run_single_child(
|
|
|
|
|
task_index=0,
|
|
|
|
|
goal=t["goal"],
|
|
|
|
|
context=t.get("context"),
|
|
|
|
|
toolsets=t.get("toolsets") or toolsets,
|
feat: configurable subagent provider:model with full credential resolution
Adds delegation.model and delegation.provider config fields so subagents
can run on a completely different provider:model pair than the parent agent.
When delegation.provider is set, the system resolves the full credential
bundle (base_url, api_key, api_mode) via resolve_runtime_provider() —
the same path used by CLI/gateway startup. This means all configured
providers work out of the box: openrouter, nous, zai, kimi-coding,
minimax, minimax-cn.
Key design decisions:
- Provider resolution uses hermes_cli.runtime_provider (single source of
truth for credential resolution across CLI, gateway, cron, and now
delegation)
- When only delegation.model is set (no provider), the model name changes
but parent credentials are inherited (for switching models within the
same provider like OpenRouter)
- When delegation.provider is set, full credentials are resolved
independently — enabling cross-provider delegation (e.g. parent on
Nous Portal, subagents on OpenRouter)
- Clear error messages if provider resolution fails (missing API key,
unknown provider name)
- _load_config() now falls back to hermes_cli.config.load_config() for
gateway/cron contexts where CLI_CONFIG is unavailable
Based on PR #791 by 0xbyt4 (closes #609), reworked to use proper
provider credential resolution instead of passing provider as metadata.
Co-authored-by: 0xbyt4 <0xbyt4@users.noreply.github.com>
2026-03-11 06:12:21 -07:00
|
|
|
model=creds["model"],
|
2026-02-20 03:15:53 -08:00
|
|
|
max_iterations=effective_max_iter,
|
|
|
|
|
parent_agent=parent_agent,
|
2026-02-28 23:29:49 -08:00
|
|
|
task_count=1,
|
feat: configurable subagent provider:model with full credential resolution
Adds delegation.model and delegation.provider config fields so subagents
can run on a completely different provider:model pair than the parent agent.
When delegation.provider is set, the system resolves the full credential
bundle (base_url, api_key, api_mode) via resolve_runtime_provider() —
the same path used by CLI/gateway startup. This means all configured
providers work out of the box: openrouter, nous, zai, kimi-coding,
minimax, minimax-cn.
Key design decisions:
- Provider resolution uses hermes_cli.runtime_provider (single source of
truth for credential resolution across CLI, gateway, cron, and now
delegation)
- When only delegation.model is set (no provider), the model name changes
but parent credentials are inherited (for switching models within the
same provider like OpenRouter)
- When delegation.provider is set, full credentials are resolved
independently — enabling cross-provider delegation (e.g. parent on
Nous Portal, subagents on OpenRouter)
- Clear error messages if provider resolution fails (missing API key,
unknown provider name)
- _load_config() now falls back to hermes_cli.config.load_config() for
gateway/cron contexts where CLI_CONFIG is unavailable
Based on PR #791 by 0xbyt4 (closes #609), reworked to use proper
provider credential resolution instead of passing provider as metadata.
Co-authored-by: 0xbyt4 <0xbyt4@users.noreply.github.com>
2026-03-11 06:12:21 -07:00
|
|
|
override_provider=creds["provider"],
|
|
|
|
|
override_base_url=creds["base_url"],
|
|
|
|
|
override_api_key=creds["api_key"],
|
|
|
|
|
override_api_mode=creds["api_mode"],
|
2026-02-20 03:15:53 -08:00
|
|
|
)
|
|
|
|
|
results.append(result)
|
|
|
|
|
else:
|
2026-02-20 03:23:23 -08:00
|
|
|
# Batch -- run in parallel with per-task progress lines
|
|
|
|
|
completed_count = 0
|
|
|
|
|
spinner_ref = getattr(parent_agent, '_delegate_spinner', None)
|
|
|
|
|
|
2026-02-24 04:13:32 -08:00
|
|
|
# Save stdout/stderr before the executor — redirect_stdout in child
|
|
|
|
|
# threads races on sys.stdout and can leave it as devnull permanently.
|
|
|
|
|
_saved_stdout = sys.stdout
|
|
|
|
|
_saved_stderr = sys.stderr
|
|
|
|
|
|
2026-02-20 03:15:53 -08:00
|
|
|
with ThreadPoolExecutor(max_workers=MAX_CONCURRENT_CHILDREN) as executor:
|
|
|
|
|
futures = {}
|
|
|
|
|
for i, t in enumerate(task_list):
|
|
|
|
|
future = executor.submit(
|
|
|
|
|
_run_single_child,
|
|
|
|
|
task_index=i,
|
|
|
|
|
goal=t["goal"],
|
|
|
|
|
context=t.get("context"),
|
|
|
|
|
toolsets=t.get("toolsets") or toolsets,
|
feat: configurable subagent provider:model with full credential resolution
Adds delegation.model and delegation.provider config fields so subagents
can run on a completely different provider:model pair than the parent agent.
When delegation.provider is set, the system resolves the full credential
bundle (base_url, api_key, api_mode) via resolve_runtime_provider() —
the same path used by CLI/gateway startup. This means all configured
providers work out of the box: openrouter, nous, zai, kimi-coding,
minimax, minimax-cn.
Key design decisions:
- Provider resolution uses hermes_cli.runtime_provider (single source of
truth for credential resolution across CLI, gateway, cron, and now
delegation)
- When only delegation.model is set (no provider), the model name changes
but parent credentials are inherited (for switching models within the
same provider like OpenRouter)
- When delegation.provider is set, full credentials are resolved
independently — enabling cross-provider delegation (e.g. parent on
Nous Portal, subagents on OpenRouter)
- Clear error messages if provider resolution fails (missing API key,
unknown provider name)
- _load_config() now falls back to hermes_cli.config.load_config() for
gateway/cron contexts where CLI_CONFIG is unavailable
Based on PR #791 by 0xbyt4 (closes #609), reworked to use proper
provider credential resolution instead of passing provider as metadata.
Co-authored-by: 0xbyt4 <0xbyt4@users.noreply.github.com>
2026-03-11 06:12:21 -07:00
|
|
|
model=creds["model"],
|
2026-02-20 03:15:53 -08:00
|
|
|
max_iterations=effective_max_iter,
|
|
|
|
|
parent_agent=parent_agent,
|
2026-02-28 23:29:49 -08:00
|
|
|
task_count=n_tasks,
|
feat: configurable subagent provider:model with full credential resolution
Adds delegation.model and delegation.provider config fields so subagents
can run on a completely different provider:model pair than the parent agent.
When delegation.provider is set, the system resolves the full credential
bundle (base_url, api_key, api_mode) via resolve_runtime_provider() —
the same path used by CLI/gateway startup. This means all configured
providers work out of the box: openrouter, nous, zai, kimi-coding,
minimax, minimax-cn.
Key design decisions:
- Provider resolution uses hermes_cli.runtime_provider (single source of
truth for credential resolution across CLI, gateway, cron, and now
delegation)
- When only delegation.model is set (no provider), the model name changes
but parent credentials are inherited (for switching models within the
same provider like OpenRouter)
- When delegation.provider is set, full credentials are resolved
independently — enabling cross-provider delegation (e.g. parent on
Nous Portal, subagents on OpenRouter)
- Clear error messages if provider resolution fails (missing API key,
unknown provider name)
- _load_config() now falls back to hermes_cli.config.load_config() for
gateway/cron contexts where CLI_CONFIG is unavailable
Based on PR #791 by 0xbyt4 (closes #609), reworked to use proper
provider credential resolution instead of passing provider as metadata.
Co-authored-by: 0xbyt4 <0xbyt4@users.noreply.github.com>
2026-03-11 06:12:21 -07:00
|
|
|
override_provider=creds["provider"],
|
|
|
|
|
override_base_url=creds["base_url"],
|
|
|
|
|
override_api_key=creds["api_key"],
|
|
|
|
|
override_api_mode=creds["api_mode"],
|
2026-02-20 03:15:53 -08:00
|
|
|
)
|
|
|
|
|
futures[future] = i
|
|
|
|
|
|
|
|
|
|
for future in as_completed(futures):
|
|
|
|
|
try:
|
2026-02-20 03:23:23 -08:00
|
|
|
entry = future.result()
|
2026-02-20 03:15:53 -08:00
|
|
|
except Exception as exc:
|
|
|
|
|
idx = futures[future]
|
2026-02-20 03:23:23 -08:00
|
|
|
entry = {
|
2026-02-20 03:15:53 -08:00
|
|
|
"task_index": idx,
|
|
|
|
|
"status": "error",
|
|
|
|
|
"summary": None,
|
|
|
|
|
"error": str(exc),
|
|
|
|
|
"api_calls": 0,
|
|
|
|
|
"duration_seconds": 0,
|
2026-02-20 03:23:23 -08:00
|
|
|
}
|
|
|
|
|
results.append(entry)
|
|
|
|
|
completed_count += 1
|
|
|
|
|
|
2026-02-28 23:29:49 -08:00
|
|
|
# Print per-task completion line above the spinner
|
2026-02-20 03:23:23 -08:00
|
|
|
idx = entry["task_index"]
|
|
|
|
|
label = task_labels[idx] if idx < len(task_labels) else f"Task {idx}"
|
|
|
|
|
dur = entry.get("duration_seconds", 0)
|
|
|
|
|
status = entry.get("status", "?")
|
|
|
|
|
icon = "✓" if status == "completed" else "✗"
|
|
|
|
|
remaining = n_tasks - completed_count
|
2026-02-28 23:29:49 -08:00
|
|
|
completion_line = f"{icon} [{idx+1}/{n_tasks}] {label} ({dur}s)"
|
|
|
|
|
if spinner_ref:
|
|
|
|
|
try:
|
|
|
|
|
spinner_ref.print_above(completion_line)
|
|
|
|
|
except Exception:
|
|
|
|
|
print(f" {completion_line}")
|
|
|
|
|
else:
|
|
|
|
|
print(f" {completion_line}")
|
2026-02-20 03:23:23 -08:00
|
|
|
|
|
|
|
|
# Update spinner text to show remaining count
|
|
|
|
|
if spinner_ref and remaining > 0:
|
|
|
|
|
try:
|
|
|
|
|
spinner_ref.update_text(f"🔀 {remaining} task{'s' if remaining != 1 else ''} remaining")
|
2026-03-10 06:59:20 -07:00
|
|
|
except Exception as e:
|
|
|
|
|
logger.debug("Spinner update_text failed: %s", e)
|
2026-02-20 03:15:53 -08:00
|
|
|
|
2026-02-24 04:13:32 -08:00
|
|
|
# Restore stdout/stderr in case redirect_stdout race left them as devnull
|
|
|
|
|
sys.stdout = _saved_stdout
|
|
|
|
|
sys.stderr = _saved_stderr
|
|
|
|
|
|
2026-02-20 03:15:53 -08:00
|
|
|
# Sort by task_index so results match input order
|
|
|
|
|
results.sort(key=lambda r: r["task_index"])
|
|
|
|
|
|
|
|
|
|
total_duration = round(time.monotonic() - overall_start, 2)
|
|
|
|
|
|
|
|
|
|
return json.dumps({
|
|
|
|
|
"results": results,
|
|
|
|
|
"total_duration_seconds": total_duration,
|
|
|
|
|
}, ensure_ascii=False)
|
|
|
|
|
|
|
|
|
|
|
feat: configurable subagent provider:model with full credential resolution
Adds delegation.model and delegation.provider config fields so subagents
can run on a completely different provider:model pair than the parent agent.
When delegation.provider is set, the system resolves the full credential
bundle (base_url, api_key, api_mode) via resolve_runtime_provider() —
the same path used by CLI/gateway startup. This means all configured
providers work out of the box: openrouter, nous, zai, kimi-coding,
minimax, minimax-cn.
Key design decisions:
- Provider resolution uses hermes_cli.runtime_provider (single source of
truth for credential resolution across CLI, gateway, cron, and now
delegation)
- When only delegation.model is set (no provider), the model name changes
but parent credentials are inherited (for switching models within the
same provider like OpenRouter)
- When delegation.provider is set, full credentials are resolved
independently — enabling cross-provider delegation (e.g. parent on
Nous Portal, subagents on OpenRouter)
- Clear error messages if provider resolution fails (missing API key,
unknown provider name)
- _load_config() now falls back to hermes_cli.config.load_config() for
gateway/cron contexts where CLI_CONFIG is unavailable
Based on PR #791 by 0xbyt4 (closes #609), reworked to use proper
provider credential resolution instead of passing provider as metadata.
Co-authored-by: 0xbyt4 <0xbyt4@users.noreply.github.com>
2026-03-11 06:12:21 -07:00
|
|
|
def _resolve_delegation_credentials(cfg: dict, parent_agent) -> dict:
|
|
|
|
|
"""Resolve credentials for subagent delegation.
|
|
|
|
|
|
|
|
|
|
If ``delegation.provider`` is configured, resolves the full credential
|
|
|
|
|
bundle (base_url, api_key, api_mode, provider) via the runtime provider
|
|
|
|
|
system — the same path used by CLI/gateway startup. This lets subagents
|
|
|
|
|
run on a completely different provider:model pair.
|
|
|
|
|
|
|
|
|
|
If no provider is configured, returns None values so the child inherits
|
|
|
|
|
everything from the parent agent.
|
|
|
|
|
|
|
|
|
|
Raises ValueError with a user-friendly message on credential failure.
|
|
|
|
|
"""
|
|
|
|
|
configured_model = cfg.get("model") or None
|
|
|
|
|
configured_provider = cfg.get("provider") or None
|
|
|
|
|
|
|
|
|
|
if not configured_provider:
|
|
|
|
|
# No provider override — child inherits everything from parent
|
|
|
|
|
return {
|
|
|
|
|
"model": configured_model,
|
|
|
|
|
"provider": None,
|
|
|
|
|
"base_url": None,
|
|
|
|
|
"api_key": None,
|
|
|
|
|
"api_mode": None,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Provider is configured — resolve full credentials
|
|
|
|
|
try:
|
|
|
|
|
from hermes_cli.runtime_provider import resolve_runtime_provider
|
|
|
|
|
runtime = resolve_runtime_provider(requested=configured_provider)
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
raise ValueError(
|
|
|
|
|
f"Cannot resolve delegation provider '{configured_provider}': {exc}. "
|
|
|
|
|
f"Check that the provider is configured (API key set, valid provider name). "
|
|
|
|
|
f"Available providers: openrouter, nous, zai, kimi-coding, minimax."
|
|
|
|
|
) from exc
|
|
|
|
|
|
|
|
|
|
api_key = runtime.get("api_key", "")
|
|
|
|
|
if not api_key:
|
|
|
|
|
raise ValueError(
|
|
|
|
|
f"Delegation provider '{configured_provider}' resolved but has no API key. "
|
|
|
|
|
f"Set the appropriate environment variable or run 'hermes login'."
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
"model": configured_model,
|
|
|
|
|
"provider": runtime.get("provider"),
|
|
|
|
|
"base_url": runtime.get("base_url"),
|
|
|
|
|
"api_key": api_key,
|
|
|
|
|
"api_mode": runtime.get("api_mode"),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2026-02-20 03:15:53 -08:00
|
|
|
def _load_config() -> dict:
|
feat: configurable subagent provider:model with full credential resolution
Adds delegation.model and delegation.provider config fields so subagents
can run on a completely different provider:model pair than the parent agent.
When delegation.provider is set, the system resolves the full credential
bundle (base_url, api_key, api_mode) via resolve_runtime_provider() —
the same path used by CLI/gateway startup. This means all configured
providers work out of the box: openrouter, nous, zai, kimi-coding,
minimax, minimax-cn.
Key design decisions:
- Provider resolution uses hermes_cli.runtime_provider (single source of
truth for credential resolution across CLI, gateway, cron, and now
delegation)
- When only delegation.model is set (no provider), the model name changes
but parent credentials are inherited (for switching models within the
same provider like OpenRouter)
- When delegation.provider is set, full credentials are resolved
independently — enabling cross-provider delegation (e.g. parent on
Nous Portal, subagents on OpenRouter)
- Clear error messages if provider resolution fails (missing API key,
unknown provider name)
- _load_config() now falls back to hermes_cli.config.load_config() for
gateway/cron contexts where CLI_CONFIG is unavailable
Based on PR #791 by 0xbyt4 (closes #609), reworked to use proper
provider credential resolution instead of passing provider as metadata.
Co-authored-by: 0xbyt4 <0xbyt4@users.noreply.github.com>
2026-03-11 06:12:21 -07:00
|
|
|
"""Load delegation config from CLI_CONFIG or persistent config.
|
|
|
|
|
|
|
|
|
|
Checks the runtime config (cli.py CLI_CONFIG) first, then falls back
|
|
|
|
|
to the persistent config (hermes_cli/config.py load_config()) so that
|
|
|
|
|
``delegation.model`` / ``delegation.provider`` are picked up regardless
|
|
|
|
|
of the entry point (CLI, gateway, cron).
|
|
|
|
|
"""
|
2026-02-20 03:15:53 -08:00
|
|
|
try:
|
|
|
|
|
from cli import CLI_CONFIG
|
feat: configurable subagent provider:model with full credential resolution
Adds delegation.model and delegation.provider config fields so subagents
can run on a completely different provider:model pair than the parent agent.
When delegation.provider is set, the system resolves the full credential
bundle (base_url, api_key, api_mode) via resolve_runtime_provider() —
the same path used by CLI/gateway startup. This means all configured
providers work out of the box: openrouter, nous, zai, kimi-coding,
minimax, minimax-cn.
Key design decisions:
- Provider resolution uses hermes_cli.runtime_provider (single source of
truth for credential resolution across CLI, gateway, cron, and now
delegation)
- When only delegation.model is set (no provider), the model name changes
but parent credentials are inherited (for switching models within the
same provider like OpenRouter)
- When delegation.provider is set, full credentials are resolved
independently — enabling cross-provider delegation (e.g. parent on
Nous Portal, subagents on OpenRouter)
- Clear error messages if provider resolution fails (missing API key,
unknown provider name)
- _load_config() now falls back to hermes_cli.config.load_config() for
gateway/cron contexts where CLI_CONFIG is unavailable
Based on PR #791 by 0xbyt4 (closes #609), reworked to use proper
provider credential resolution instead of passing provider as metadata.
Co-authored-by: 0xbyt4 <0xbyt4@users.noreply.github.com>
2026-03-11 06:12:21 -07:00
|
|
|
cfg = CLI_CONFIG.get("delegation", {})
|
|
|
|
|
if cfg:
|
|
|
|
|
return cfg
|
|
|
|
|
except Exception:
|
|
|
|
|
pass
|
|
|
|
|
try:
|
|
|
|
|
from hermes_cli.config import load_config
|
|
|
|
|
full = load_config()
|
|
|
|
|
return full.get("delegation", {})
|
2026-02-20 03:15:53 -08:00
|
|
|
except Exception:
|
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
# OpenAI Function-Calling Schema
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
DELEGATE_TASK_SCHEMA = {
|
|
|
|
|
"name": "delegate_task",
|
|
|
|
|
"description": (
|
|
|
|
|
"Spawn one or more subagents to work on tasks in isolated contexts. "
|
|
|
|
|
"Each subagent gets its own conversation, terminal session, and toolset. "
|
|
|
|
|
"Only the final summary is returned -- intermediate tool results "
|
|
|
|
|
"never enter your context window.\n\n"
|
2026-02-21 02:41:30 -08:00
|
|
|
"TWO MODES (one of 'goal' or 'tasks' is required):\n"
|
2026-02-20 03:15:53 -08:00
|
|
|
"1. Single task: provide 'goal' (+ optional context, toolsets)\n"
|
|
|
|
|
"2. Batch (parallel): provide 'tasks' array with up to 3 items. "
|
|
|
|
|
"All run concurrently and results are returned together.\n\n"
|
|
|
|
|
"WHEN TO USE delegate_task:\n"
|
|
|
|
|
"- Reasoning-heavy subtasks (debugging, code review, research synthesis)\n"
|
|
|
|
|
"- Tasks that would flood your context with intermediate data\n"
|
|
|
|
|
"- Parallel independent workstreams (research A and B simultaneously)\n\n"
|
|
|
|
|
"WHEN NOT TO USE (use these instead):\n"
|
|
|
|
|
"- Mechanical multi-step work with no reasoning needed -> use execute_code\n"
|
|
|
|
|
"- Single tool call -> just call the tool directly\n"
|
|
|
|
|
"- Tasks needing user interaction -> subagents cannot use clarify\n\n"
|
|
|
|
|
"IMPORTANT:\n"
|
|
|
|
|
"- Subagents have NO memory of your conversation. Pass all relevant "
|
|
|
|
|
"info (file paths, error messages, constraints) via the 'context' field.\n"
|
|
|
|
|
"- Subagents CANNOT call: delegate_task, clarify, memory, send_message, "
|
|
|
|
|
"execute_code.\n"
|
|
|
|
|
"- Each subagent gets its own terminal session (separate working directory and state).\n"
|
|
|
|
|
"- Results are always returned as an array, one entry per task."
|
|
|
|
|
),
|
|
|
|
|
"parameters": {
|
|
|
|
|
"type": "object",
|
|
|
|
|
"properties": {
|
|
|
|
|
"goal": {
|
|
|
|
|
"type": "string",
|
|
|
|
|
"description": (
|
|
|
|
|
"What the subagent should accomplish. Be specific and "
|
|
|
|
|
"self-contained -- the subagent knows nothing about your "
|
|
|
|
|
"conversation history."
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
"context": {
|
|
|
|
|
"type": "string",
|
|
|
|
|
"description": (
|
|
|
|
|
"Background information the subagent needs: file paths, "
|
|
|
|
|
"error messages, project structure, constraints. The more "
|
|
|
|
|
"specific you are, the better the subagent performs."
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
"toolsets": {
|
|
|
|
|
"type": "array",
|
|
|
|
|
"items": {"type": "string"},
|
|
|
|
|
"description": (
|
|
|
|
|
"Toolsets to enable for this subagent. "
|
2026-03-06 17:36:06 -08:00
|
|
|
"Default: inherits your enabled toolsets. "
|
2026-02-20 03:15:53 -08:00
|
|
|
"Common patterns: ['terminal', 'file'] for code work, "
|
|
|
|
|
"['web'] for research, ['terminal', 'file', 'web'] for "
|
|
|
|
|
"full-stack tasks."
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
"tasks": {
|
|
|
|
|
"type": "array",
|
|
|
|
|
"items": {
|
|
|
|
|
"type": "object",
|
|
|
|
|
"properties": {
|
|
|
|
|
"goal": {"type": "string", "description": "Task goal"},
|
|
|
|
|
"context": {"type": "string", "description": "Task-specific context"},
|
|
|
|
|
"toolsets": {
|
|
|
|
|
"type": "array",
|
|
|
|
|
"items": {"type": "string"},
|
|
|
|
|
"description": "Toolsets for this specific task",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
"required": ["goal"],
|
|
|
|
|
},
|
|
|
|
|
"maxItems": 3,
|
|
|
|
|
"description": (
|
|
|
|
|
"Batch mode: up to 3 tasks to run in parallel. Each gets "
|
|
|
|
|
"its own subagent with isolated context and terminal session. "
|
|
|
|
|
"When provided, top-level goal/context/toolsets are ignored."
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
"max_iterations": {
|
|
|
|
|
"type": "integer",
|
|
|
|
|
"description": (
|
2026-03-02 00:51:10 -08:00
|
|
|
"Max tool-calling turns per subagent (default: 50). "
|
|
|
|
|
"Only set lower for simple tasks."
|
2026-02-20 03:15:53 -08:00
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
"required": [],
|
|
|
|
|
},
|
|
|
|
|
}
|
2026-02-21 20:22:33 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# --- Registry ---
|
|
|
|
|
from tools.registry import registry
|
|
|
|
|
|
|
|
|
|
registry.register(
|
|
|
|
|
name="delegate_task",
|
|
|
|
|
toolset="delegation",
|
|
|
|
|
schema=DELEGATE_TASK_SCHEMA,
|
|
|
|
|
handler=lambda args, **kw: delegate_task(
|
|
|
|
|
goal=args.get("goal"),
|
|
|
|
|
context=args.get("context"),
|
|
|
|
|
toolsets=args.get("toolsets"),
|
|
|
|
|
tasks=args.get("tasks"),
|
|
|
|
|
max_iterations=args.get("max_iterations"),
|
|
|
|
|
parent_agent=kw.get("parent_agent")),
|
|
|
|
|
check_fn=check_delegate_requirements,
|
|
|
|
|
)
|