The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(tools/delegate): split delegate_tool into child_run/config/dispatch/progress/registry/results; compact delegation helpers
Teknium05526b028abb
diff --git a/tests/tools/test_async_delegation.py b/tests/tools/test_async_delegation.pyindex 9c9766a7a1a..edb150e7943 100644--- a/tests/tools/test_async_delegation.py+++ b/tests/tools/test_async_delegation.py@@ -854,10 +854,10 @@ def _patch_delegation_cfg(monkeypatch, model="upstage/solar-pro-4", provider="op"""Pin the delegation config the notice renderer reads (adapts the#97667 tests to the shipped implementation, which reads the configuredmodel from config rather than the event's model field)."""- import tools.process_registry as _pr+ import tools.process_registry_notifications as _prnmonkeypatch.setattr(- _pr, "_delegation_config", lambda: {"model": model, "provider": provider}+ _prn, "_delegation_config", lambda: {"model": model, "provider": provider})diff --git a/tools/budget_config.py b/tools/budget_config.pyindex ec02576a763..7cebc015060 100644--- a/tools/budget_config.py+++ b/tools/budget_config.py@@ -12,52 +12,34 @@ PINNED_THRESHOLDS: Dict[str, float] = {"read_file": float("inf"),}-# Defaults matching the current hardcoded values in tool_result_storage.py.-# Kept here as the single source of truth; tool_result_storage.py imports these.+# Single source of truth for the defaults; tool_result_storage.py imports these.DEFAULT_RESULT_SIZE_CHARS: int = 100_000DEFAULT_TURN_BUDGET_CHARS: int = 200_000DEFAULT_PREVIEW_SIZE_CHARS: int = 1_500-# Tighter default per-result threshold for MCP tools (name prefix ``mcp_``).-#-# MCP servers routinely return un-paginated 20-50K-char payloads (tool-# discovery catalogs, batched executions) that sail under the generic 100K-# threshold and silently bloat context — in agentic evals this measurably-# ballooned per-turn reasoning time on long conversations. Competitor-# harnesses cap harder (OpenCode 50KB, pi 50KB, Claude Code 30K chars,-# Codex ~10K tokens); 50K chars keeps parity with the strictest general--# purpose caps while spillover (unlike truncation) preserves the full-# payload on disk. Overridable via ``tool_budget.mcp_result_size_chars``-# in config.yaml.+# Tighter per-result default for MCP tools (``mcp_`` prefix): MCP servers+# routinely return un-paginated 20-50K-char payloads that sail under the+# generic 100K threshold and bloat context. 50K matches the strictest general+# competitor caps while spillover (unlike truncation) keeps the full payload on+# disk. Overridable via ``tool_budget.mcp_result_size_chars`` in config.yaml.DEFAULT_MCP_RESULT_SIZE_CHARS: int = 50_000-# Tool-name prefix that identifies MCP-served tools (same prefix the-# untrusted-content wrapper keys on in agent/tool_dispatch_helpers.py).+# Same prefix the untrusted-content wrapper keys on (agent/tool_dispatch_helpers.py).MCP_TOOL_PREFIX: str = "mcp_"def _configured_mcp_result_size() -> int:- """Read ``tool_budget.mcp_result_size_chars`` from the active config.-- Goes through :func:`hermes_cli.config.load_config_readonly` (the- sanctioned read path — raw config.yaml parsing outside owner modules- is guarded by tests/hermes_cli/test_config_read_guard.py). Fully- guarded: any error, missing key, or non-positive value returns the- built-in default. The ``tool_budget:`` block name is shared with the- wider configurable-caps proposal (#80508) so the two can merge- without a key rename.- """+ """Read ``tool_budget.mcp_result_size_chars`` via ``load_config_readonly`` (the+ sanctioned read path; raw config.yaml parsing outside owner modules is test-guarded).+ Any error, missing key or non-positive value returns the built-in default."""try:from hermes_cli.config import load_config_readonlydata = load_config_readonly()block = data.get("tool_budget") if isinstance(data, dict) else None- if isinstance(block, dict):- raw = block.get("mcp_result_size_chars")- if raw is not None:- value = int(raw)- if value > 0:- return value+ raw = block.get("mcp_result_size_chars") if isinstance(block, dict) else None+ if raw is not None and int(raw) > 0:+ return int(raw)except Exception:passreturn DEFAULT_MCP_RESULT_SIZE_CHARS@@ -68,8 +50,7 @@ class BudgetConfig:"""Immutable budget constants for the 3-layer tool result persistence system.Layer 2 (per-result): resolve_threshold(tool_name) -> threshold in chars.- Layer 3 (per-turn): turn_budget -> aggregate char budget across all tool- results in a single assistant turn.+ Layer 3 (per-turn): turn_budget -> aggregate char budget across one assistant turn.Preview: preview_size -> inline snippet size after persistence."""@@ -80,24 +61,15 @@ class BudgetConfig:tool_overrides: Dict[str, int] = field(default_factory=dict)def resolve_threshold(self, tool_name: str) -> int | float:- """Resolve the persistence threshold for a tool.-- Priority: pinned -> tool_overrides -> mcp_ prefix -> registry- per-tool -> default.-- MCP tools (``mcp_`` prefix) get a tighter default threshold- (``mcp_result_size``, 50K chars) because MCP servers return- un-paginated payloads with no per-tool registry entry to constrain- them. The value is additionally capped at ``default_result_size``- so a context-scaled budget for a small model still constrains MCP- results the same way it constrains registry values.-- The registry per-tool value is capped at ``default_result_size`` so a- context-scaled budget (small model) actually constrains tools that- register a large fixed ``max_result_size_chars`` (web/terminal/x_search- all register 100K). For the default budget this is a no-op because both- equal 100K; for a scaled-down budget it prevents a per-tool registry- value from re-inflating the cap past the model's window (#23767).+ """Priority: pinned -> tool_overrides -> mcp_ prefix -> registry per-tool -> default.++ MCP tools get ``mcp_result_size`` because they have no per-tool registry+ entry to constrain them. Both the MCP value and the registry value are+ capped at ``default_result_size`` so a context-scaled budget (small+ model) still constrains tools that register a large fixed+ ``max_result_size_chars`` (web/terminal/x_search all register 100K);+ a no-op for the default budget, but for a scaled-down budget it stops a+ registry value from re-inflating the cap past the model's window."""if tool_name in PINNED_THRESHOLDS:return PINNED_THRESHOLDS[tool_name]@@ -112,44 +84,30 @@ class BudgetConfig:return min(registry_value, self.default_result_size)-# Default config -- matches current hardcoded behavior exactly.+# Default config -- matches the historical hardcoded behavior exactly.DEFAULT_BUDGET = BudgetConfig()-# Token<->char conversion used when scaling the budget to a model's context-# window. Deliberately conservative (a smaller divisor = more chars per token =-# a larger char budget) would UNDER-protect small models, so we use the same-# rough 4-chars-per-token ratio the estimator uses (agent/model_metadata.py).+# Token<->char ratio for scaling to a context window; same rough 4-chars-per-token+# the estimator uses (agent/model_metadata.py). A smaller divisor would+# UNDER-protect small models._CHARS_PER_TOKEN: int = 4-# Fraction of a model's context window we allow a SINGLE tool result to occupy-# before persisting/truncating it, and the fraction the WHOLE turn's tool-# output may occupy. Tool output is not the only thing in the window (system-# prompt, tool schemas, conversation history, the model's own reply all-# compete), so these stay well under 1.0.+# Window fraction a SINGLE tool result / the WHOLE turn's tool output may occupy.+# System prompt, tool schemas, history and the reply all compete, so well under 1.0._PER_RESULT_WINDOW_FRACTION: float = 0.15_PER_TURN_WINDOW_FRACTION: float = 0.30-# Floor so even a tiny-but-admitted model still gets a usable preview/result-# rather than a 0-char budget.+# Floors so a tiny model still gets a usable preview/result, never a 0-char budget._MIN_RESULT_SIZE_CHARS: int = 8_000_MIN_TURN_BUDGET_CHARS: int = 16_000def budget_for_context_window(context_length: int | None) -> BudgetConfig:- """Return a BudgetConfig scaled to the active model's context window.-- The fixed defaults (100K result / 200K turn chars) are correct for large- (200K+ token) models but blind to small ones: on a 65K-token model a single- tool result persisted at the 100K-char threshold, or a 200K-char turn- budget (~50K tokens), can by itself approach or exceed the whole window and- force an oversized request (#23767).-- Scaling keeps large models byte-identical to today (the proportional value- is clamped to the existing defaults as a CAP) while shrinking the budget for- small models proportionally to their window, floored so a usable preview- always survives.- """+ """Return a BudgetConfig scaled to the active model's context window. The fixed+ defaults suit 200K+ token models but on a 65K model one result/turn can fill+ the window; the proportional value is clamped to the defaults as a CAP (large+ models stay byte-identical) and floored so a usable preview always survives."""mcp_result_size = _configured_mcp_result_size()if not context_length or context_length <= 0:@@ -158,17 +116,9 @@ def budget_for_context_window(context_length: int | None) -> BudgetConfig:return BudgetConfig(mcp_result_size=mcp_result_size)window_chars = context_length * _CHARS_PER_TOKEN- per_result = int(window_chars * _PER_RESULT_WINDOW_FRACTION)- per_turn = int(window_chars * _PER_TURN_WINDOW_FRACTION)-- # Clamp: never exceed the historical defaults (so large models are- # unchanged), never drop below the floor (so tiny models stay usable).- per_result = max(_MIN_RESULT_SIZE_CHARS, min(per_result, DEFAULT_RESULT_SIZE_CHARS))- per_turn = max(_MIN_TURN_BUDGET_CHARS, min(per_turn, DEFAULT_TURN_BUDGET_CHARS))-return BudgetConfig(- default_result_size=per_result,- turn_budget=per_turn,+ default_result_size=max(_MIN_RESULT_SIZE_CHARS, min(int(window_chars * _PER_RESULT_WINDOW_FRACTION), DEFAULT_RESULT_SIZE_CHARS)),+ turn_budget=max(_MIN_TURN_BUDGET_CHARS, min(int(window_chars * _PER_TURN_WINDOW_FRACTION), DEFAULT_TURN_BUDGET_CHARS)),preview_size=DEFAULT_PREVIEW_SIZE_CHARS,mcp_result_size=mcp_result_size,)diff --git a/tools/delegate_tool.py b/tools/delegate_tool.pyindex 796487f0411..30128f58d7d 100644--- a/tools/delegate_tool.py+++ b/tools/delegate_tool.py@@ -17,33 +17,100 @@ The parent's context only sees the delegation call and the summary result,never the child's intermediate tool calls or reasoning."""-import enumimport contextvarsimport jsonimport loggingimport re--logger = logging.getLogger(__name__)-import osimport threadingimport timeimport weakref-from concurrent.futures import (- TimeoutError as FuturesTimeoutError,-)+from dataclasses import dataclassfrom typing import Any, Dict, List, Optional-from urllib.parse import urlsplit, urlunsplitfrom toolsets import TOOLSETS-from agent.interrupt_compat import request_hard_interrupt-# Sentinel value used by the runtime provider system for providers that are-# not natively known (named custom providers, third-party aggregators, etc.).-# Must match hermes_cli.runtime_provider.RUNTIME_PROVIDER_TYPE_CUSTOM.-_RUNTIME_PROVIDER_CUSTOM = "custom"-from tools import file_statefrom tools.terminal_tool import set_approval_callback as _set_subagent_approval_cb-from utils import base_url_hostname, is_truthy_value+from utils import is_truthy_value++logger = logging.getLogger(__name__)++# The delegate_tool_* siblings hold the pieces split out of this module. Every+# moved name is re-imported here so ``tools.delegate_tool.<name>`` keeps+# resolving for callers and for tests that patch it. Mutable flag globals+# (_spawn_paused, *_WARNED) live only in their owning module.+from tools.delegate_tool_child_run import ( # noqa: F401+ _WorktreeReporter,+ _append_sibling_write_reminder,+ _build_result_entry,+ _cleanup_child_run,+ _dump_subagent_timeout_diagnostic,+ _emit_child_complete,+ _handle_child_wait_failure,+ _register_child,+ _seed_child_workspace,+ _start_heartbeat,+ _validate_child_output_schema,+)+from tools.delegate_tool_config import ( # noqa: F401+ _DEFAULT_MAX_CONCURRENT_CHILDREN,+ _get_child_timeout,+ _get_inherit_mcp_toolsets,+ _get_max_async_children,+ _get_max_concurrent_children,+ _get_max_spawn_depth,+ _get_orchestrator_enabled,+ _get_subagent_approval_callback,+ _get_worktree_isolation,+ _inherit_parent_base_url,+ _inherit_parent_capabilities,+ _load_config,+ _merge_request_overrides,+ _resolve_child_credential_pool,+ _resolve_delegation_credentials,+ _subagent_auto_approve,+ _subagent_auto_deny,+)+from tools.delegate_tool_dispatch import ( # noqa: F401+ _dispatch_background,+ _run_children_parallel,+)+from tools.delegate_tool_progress import ( # noqa: F401+ DelegateEvent,+ SUBAGENT_FAILURE_STATUSES,+ _batch_prefix,+ _build_child_progress_callback,+ _build_child_system_prompt,+ _clean_error_text,+ _emit_parent_console,+ _resolve_workspace_hint,+ format_batch_tag,+ format_subagent_failure_line,+)+from tools.delegate_tool_registry import ( # noqa: F401+ _CONTROL_ACTIONS,+ _active_subagents,+ _active_subagents_lock,+ _capture_gateway_steer_authority,+ _close_subagent_steering,+ _handle_control_action,+ _is_descendant_of,+ _owns_subagent_record,+ _register_subagent,+ _unregister_subagent,+ get_subagent_attribution,+ interrupt_subagent,+ is_spawn_paused,+ list_active_subagents,+ set_spawn_paused,+ steer_subagent,+)+from tools.delegate_tool_results import ( # noqa: F401+ _apply_summary_budget,+ _build_child_preserving_parent_tools,+ _finalize_child_results,+ _run_child_lifecycle,+ _summarize_tool_arguments,+)# Tools that children must never have access to@@ -58,839 +125,12 @@ DELEGATE_BLOCKED_TOOLS = frozenset()-# ----------------------------------------------------------------------------# Subagent approval callbacks-# ----------------------------------------------------------------------------# Subagents run inside a ThreadPoolExecutor worker. The CLI's interactive-# approval callback is stored in tools/terminal_tool.py's threading.local(),-# so worker threads do NOT inherit it. Without a callback,-# prompt_dangerous_approval() falls back to input() from the worker thread,-# which deadlocks against the parent's prompt_toolkit TUI that owns stdin.-#-# Fix: install a non-interactive callback into every subagent worker thread-# via ThreadPoolExecutor(initializer=_set_subagent_approval_cb, initargs=(cb,)).-# The callback is chosen by the `delegation.subagent_auto_approve` config:-# false (default) → _subagent_auto_deny (safe; matches leaf tool blocklist)-# true → _subagent_auto_approve (opt-in YOLO for cron/batch)-# Both emit a logger.warning for audit; gateway sessions are unaffected-# because they resolve approvals via tools/approval.py's per-session queue,-# not through these TLS callbacks.-def _subagent_auto_deny(command: str, description: str, **kwargs) -> str:- """Auto-deny dangerous commands in subagent threads (safe default).-- Returns 'deny' so the subagent sees a refusal it can recover from, and- never calls input() (which would deadlock the parent TUI).- """- logger.warning(- "Subagent auto-denied dangerous command: %s (%s). "- "Set delegation.subagent_auto_approve: true to allow.",- command, description,- )- return "deny"---def _subagent_auto_approve(command: str, description: str, **kwargs) -> str:- """Auto-approve dangerous commands in subagent threads (opt-in YOLO).-- Only installed when delegation.subagent_auto_approve=true. Returns 'once'- so the subagent proceeds without blocking the parent UI.- """- logger.warning(- "Subagent auto-approved dangerous command: %s (%s)",- command, description,- )- return "once"---def _get_subagent_approval_callback():- """Return the callback to install into subagent worker threads.-- Config key: delegation.subagent_auto_approve (bool, default False).- Reads via the same _load_config() path as the rest of delegate_task so- priority is config.yaml > (no env override for this knob) > default.- """- cfg = _load_config()- val = cfg.get("subagent_auto_approve", False)- if is_truthy_value(val):- return _subagent_auto_approve- return _subagent_auto_deny--# NOTE: nested delegation is granted by role='orchestrator' (which re-adds the-# "delegation" toolset in _build_child_agent), NOT by the model naming toolsets-# — the model has no toolsets argument. Subagents inherit the parent's toolsets.--_DEFAULT_MAX_CONCURRENT_CHILDREN = 10-# One-shot guard: the high-concurrency cost advisory is emitted at most once-# per process. _get_max_concurrent_children() runs on every get_definitions()-# schema rebuild (via _build_top_level_description / _build_tasks_param_description),-# so without this flag a config of max_concurrent_children>10 spams the log on-# every turn / agent spawn even when delegate_task is never called.-_HIGH_CONCURRENCY_WARNED = False-MAX_DEPTH = 1 # flat by default: parent (0) -> child (1); grandchild rejected unless max_spawn_depth raised.-# Configurable depth cap consulted by _get_max_spawn_depth; MAX_DEPTH-# stays as the default fallback and is still the symbol tests import.-_MIN_SPAWN_DEPTH = 1-# No upper ceiling on spawn depth — like max_concurrent_children, depth has a-# floor of 1 and no ceiling. Deeper trees multiply API cost, so the default-# stays flat (MAX_DEPTH = 1); raising the config knob is an explicit opt-in.---# ----------------------------------------------------------------------------# Runtime state: pause flag + active subagent registry-#-# Consumed by the TUI observability layer (overlay/control surface) and the-# gateway RPCs `delegation.pause`, `delegation.status`, `subagent.interrupt`.-# Kept module-level so they span every delegate_task invocation in the-# process, including nested orchestrator -> worker chains.-# -----------------------------------------------------------------------------_spawn_pause_lock = threading.Lock()-_spawn_paused: bool = False--_active_subagents_lock = threading.Lock()-# subagent_id -> mutable record tracking the live child agent. Stays only-# for the lifetime of the run; _run_single_child is the owner.-_active_subagents: Dict[str, Dict[str, Any]] = {}--# subagent_id -> {goal, delegation_id, parent_session_id} retained AFTER the-# child finishes (bounded FIFO). Child-started background processes routinely-# outlive the child itself (its npm ci with notify_on_complete=true finishes-# after the child's summary was delivered); their completion notifications-# reach the parent conversation via the shared completion_queue and need-# delegation attribution even though the live registry entry is gone.-_RECENT_SUBAGENTS_CAP = 200-_recent_subagents: Dict[str, Dict[str, Any]] = {}---# Terminal child statuses that mean "the subagent did NOT deliver a usable-# result". Shared by the CLI spinner echo, the gateway failure notice, and-# the parent-facing failure summary so every surface agrees on what counts-# as a failure.-SUBAGENT_FAILURE_STATUSES = frozenset({"failed", "error", "timeout"})---def _clean_error_text(error: Any, max_chars: int = 200) -> str:- """Reduce an arbitrary error payload to one clean human-readable line.-- Provider/SDK errors routinely arrive as multi-line tracebacks or JSON- walls. For a chat-facing notice we want the single most informative- line: the exception message (last line of a traceback) or the first- non-empty line otherwise, hard-capped in length.- """- text = str(error or "").strip()- if not text:- return ""- lines = [ln.strip() for ln in text.splitlines() if ln.strip()]- if not lines:- return ""- # A traceback's last line is the actual exception message.- line = lines[-1] if lines[0].startswith("Traceback") else lines[0]- if len(line) > max_chars:- line = line[: max_chars - 3] + "..."- return line---def format_subagent_failure_line(- goal: Optional[str],- status: Optional[str],- error: Any = None,- duration_seconds: Any = None,-) -> str:- """One clean, human-readable line describing a failed subagent.-- Rendered directly to the user (CLI spinner echo, gateway platform- notice) — no JSON, no traceback, no internal field names. Example:-- ⚠️ Subagent failed — "research competitor pricing": Error code: 404 —- model not found (after 12s)- """- goal_label = (goal or "").strip().replace("\n", " ")- if len(goal_label) > 60:- goal_label = goal_label[:57] + "..."- verb = "timed out" if status == "timeout" else "failed"- line = f"⚠️ Subagent {verb}"- if goal_label:- line += f' — "{goal_label}"'- err = _clean_error_text(error)- if err:- line += f": {err}"- if isinstance(duration_seconds, (int, float)) and duration_seconds > 0:- line += f" (after {round(duration_seconds)}s)"- return line---def get_subagent_attribution(task_id: Optional[str]) -> Optional[Dict[str, Any]]:- """Resolve a process task_id to its originating delegation, if any.-- Children run their terminal sessions under ``task_id == subagent_id``- (see _run_single_child's child_task_id), so a background process spawned- by a subagent carries that id in ``ProcessSession.task_id``. Returns- ``{subagent_id, goal, delegation_id}`` for live AND recently-finished- children, or None when the task_id is not a known subagent.- """- if not task_id or not isinstance(task_id, str):- return None- with _active_subagents_lock:- record = _active_subagents.get(task_id)- if record is not None:- return {- "subagent_id": task_id,- "goal": record.get("goal"),- "delegation_id": record.get("delegation_id"),- }- retained = _recent_subagents.get(task_id)- if retained is not None:- return {- "subagent_id": task_id,- "goal": retained.get("goal"),- "delegation_id": retained.get("delegation_id"),- }- return None---def set_spawn_paused(paused: bool) -> bool:- """Globally block/unblock new delegate_task spawns.-- Active children keep running; only NEW calls to delegate_task fail fast- with a "spawning paused" error until unblocked. Returns the new state.- """- global _spawn_paused- with _spawn_pause_lock:- _spawn_paused = bool(paused)- return _spawn_paused---def is_spawn_paused() -> bool:- with _spawn_pause_lock:- return _spawn_paused---def _register_subagent(record: Dict[str, Any]) -> None:- sid = record.get("subagent_id")- if not sid:- return- record.setdefault("accepting_steer", True)- with _active_subagents_lock:- _active_subagents[sid] = record---def _retain_recent_subagent(record: Dict[str, Any]) -> None:- """Keep a bounded attribution stub after a child finishes (lock held)."""- sid = record.get("subagent_id")- if not sid:- return- _recent_subagents[sid] = {- "goal": record.get("goal"),- "delegation_id": record.get("delegation_id"),- "owner_agent_session_id": record.get("owner_agent_session_id"),- }- while len(_recent_subagents) > _RECENT_SUBAGENTS_CAP:- _recent_subagents.pop(next(iter(_recent_subagents)), None)---def _unregister_subagent(subagent_id: str, *, agent: Any = None) -> None:- with _active_subagents_lock:- record = _active_subagents.get(subagent_id)- if record is not None and (agent is None or record.get("agent") is agent):- _active_subagents.pop(subagent_id, None)- _retain_recent_subagent(record)---def _close_subagent_steering(subagent_id: str, agent: Any) -> Optional[str]:- """Atomically close steer acceptance and drain its final durable artifact.-- ``steer_subagent`` holds the same registry lock through ``agent.steer``.- Therefore either acceptance wins and this drain sees its exact text, or- closure wins and the caller is rejected. Exact agent identity prevents a- finishing child with a recycled public id from closing its replacement.- """- with _active_subagents_lock:- record = _active_subagents.get(subagent_id)- if record is None or record.get("agent") is not agent:- return None- record["accepting_steer"] = False- drain = getattr(agent, "_drain_pending_steer", None)- if not callable(drain):- return None- try:- pending = drain()- except Exception as exc:- logger.debug("final steer drain for %s failed: %s", subagent_id, exc)- return None- return pending if isinstance(pending, str) and pending.strip() else None---def interrupt_subagent(subagent_id: str) -> bool:- """Request that a single running subagent stop at its next iteration boundary.-- Does not hard-kill the worker thread (Python can't); sets the child's- interrupt flag which propagates to in-flight tools and recurses into- grandchildren via AIAgent.interrupt(). Returns True if a matching- subagent was found.- """- with _active_subagents_lock:- record = _active_subagents.get(subagent_id)- if not record:- return False- agent = record.get("agent")- if agent is None:- return False- try:- if not request_hard_interrupt(agent, f"Interrupted via TUI ({subagent_id})"):- return False- except Exception as exc:- logger.debug("interrupt_subagent(%s) failed: %s", subagent_id, exc)- return False- return True---def steer_subagent(- subagent_id: str,- text: str,- *,- owner_session_id: Optional[str] = None,- owner_transport: Any = None,- owner_session_record: Any = None,-) -> bool:- """Queue steering text into a single running subagent without stopping it.-- The redirection-side mirror of interrupt_subagent(): resolves the live- child in the registry and calls AIAgent.steer(), which appends the text- to the child's last tool result at its next iteration boundary — the- current tool call is never cut. Returns True if a matching subagent- QUEUED the text while the child was still accepting work; False for an- unknown/closed id, an ownership mismatch, a record with no live agent, or- empty text. ``owner_session_id=None`` deliberately preserves the internal- in-process helper contract; gateway callers must pass exact authority.-- Acceptance and completion are linearized by the registry lock. If- acceptance wins but no delivery boundary remains, ``_run_single_child``- drains the exact text into the completion entry as ``missed_steer``.- """- if not text or not text.strip():- return False- with _active_subagents_lock:- record = _active_subagents.get(subagent_id)- if not record or not record.get("accepting_steer", False):- return False- if owner_session_id is not None:- if (- record.get("owner_session_id") != owner_session_id- or owner_transport is None- or record.get("owner_transport") is not owner_transport- or owner_session_record is None- or record.get("owner_session_record") is not owner_session_record- ):- return False- agent = record.get("agent")- if agent is None:- return False- try:- return bool(agent.steer(text))- except Exception as exc:- logger.debug("steer_subagent(%s) failed: %s", subagent_id, exc)- return False---def _capture_gateway_steer_authority(- owner_session_id: Optional[str],-) -> tuple[Any, Any]:- """Capture exact request transport + live session generation, if any.-- This is intentionally an in-process bridge, not a serializable capability.- Non-gateway hosts (including the CLI helper path) receive ``(None, None)``.- """- if not owner_session_id:- return None, None- try:- from tui_gateway.server import _current_session_steer_authority-- return _current_session_steer_authority(owner_session_id)- except Exception:- return None, None---def list_active_subagents() -> List[Dict[str, Any]]:- """Snapshot of the currently running subagent tree.-- Each record: {subagent_id, parent_id, depth, goal, model, started_at,- tool_count, status}. Safe to call from any thread — returns a copy.- """- with _active_subagents_lock:- return [- {- k: v- for k, v in r.items()- if k- not in {- "agent",- "owner_session_id",- "owner_transport",- "owner_session_record",- "accepting_steer",- }- }- for r in _active_subagents.values()- ]---def _is_descendant_of(child_agent: Any, parent_agent: Any, max_hops: int = 8) -> bool:- """True when *child_agent* sits below *parent_agent* in the spawn tree.-- Walks the ``_delegate_parent_ref`` weakref chain stamped at build time.- Identity comparison only — a parent may steer/stop its own children and- grandchildren, never a sibling tree owned by another conversation.- """- if child_agent is None or parent_agent is None:- return False- cur = child_agent- for _ in range(max_hops):- ref = getattr(cur, "_delegate_parent_ref", None)- ancestor = ref() if callable(ref) else None- if ancestor is None:- return False- if ancestor is parent_agent:- return True- cur = ancestor- return False---# Model-facing control actions accepted by delegate_task(action=...).-# "spawn" (or omitted) keeps the historical spawn semantics.-_CONTROL_ACTIONS = frozenset({"list", "steer", "stop"})---def _resolve_session_lineage(session_id: Optional[str], parent_agent: Any) -> str:- """Resolve a session id to the tip of its compression lineage.-- Best-effort: uses the parent's live SessionDB handle when present so a- delegation dispatched before a compression rotation still matches the- rotated parent. Returns the input unchanged when resolution fails.- """- sid = str(session_id or "")- if not sid:- return ""- db = getattr(parent_agent, "_session_db", None)- if db is None:- return sid- try:- resolved = db.resolve_resume_session_id(sid)- return str(resolved) if resolved else sid- except Exception:- return sid---def _owns_subagent_record(record: Dict[str, Any], parent_agent: Any) -> bool:- """True when *parent_agent*'s conversation owns this live-child record.-- Two-tier check:-- 1. Object identity — the ``_delegate_parent_ref`` weakref chain stamped- at build time reaches *parent_agent*. Fast path for the common case- where the parent AIAgent object survives the whole run.- 2. Durable conversation lineage — the child was registered with the- owning conversation's durable session id- (``owner_agent_session_id``); match it against the calling parent's- ``session_id``, resolving compression-rotation lineage on both sides.-- Tier 2 exists because the identity chain is BRITTLE across parent-agent- rebuilds: the CLI sets ``self.agent = None`` mid-session (route-signature- change, credential refresh, /model, MoA one-shots) and constructs a NEW- AIAgent for the next turn while the child keeps running with a weakref to- the old object. The delivery path always survived this (it routes by- durable session id); the control path must use the same durable spine or- running children go invisible/unsteerable (observed live: deleg_88454b70- / sa-0-dc0100f4, 2026-08-17).- """- agent = record.get("agent")- if _is_descendant_of(agent, parent_agent):- return True- owner_sid = str(record.get("owner_agent_session_id") or "")- if not owner_sid:- return False- parent_sid = str(getattr(parent_agent, "session_id", "") or "")- if not parent_sid:- return False- if owner_sid == parent_sid:- return True- # Compression rotation on either side: compare lineage tips.- return _resolve_session_lineage(owner_sid, parent_agent) in {- parent_sid,- _resolve_session_lineage(parent_sid, parent_agent),- }---def _handle_control_action(- action: str,- subagent_id: Optional[str],- message: Optional[str],- parent_agent: Any,-) -> str:- """Synchronous control plane for delegate_task: list/steer/stop.-- Runs in-turn (never backgrounded) and only over subagents descended from- *parent_agent* — the same registry the TUI overlay drives, but scoped so- a conversation can only control its own spawn tree.- """- if action == "list":- with _active_subagents_lock:- records = list(_active_subagents.values())- entries = []- for r in records:- agent = r.get("agent")- if not _owns_subagent_record(r, parent_agent):- continue- started = r.get("started_at")- entries.append(- {- "subagent_id": r.get("subagent_id"),- "parent_id": r.get("parent_id"),- "goal": r.get("goal"),- "model": r.get("model"),- "status": r.get("status"),- "running_seconds": (- round(time.time() - started, 1)- if isinstance(started, (int, float))- else None- ),- "accepting_steer": bool(r.get("accepting_steer", False)),- "live_transcript": getattr(agent, "_live_transcript_path", None),- }- )- payload: Dict[str, Any] = {- "action": "list",- "count": len(entries),- "subagents": entries,- }- if not entries:- payload["note"] = (- "No live subagents right now. Children that already finished "- "have delivered (or will deliver) their results as normal "- "completion messages — there is nothing to steer or stop."- )- return json.dumps(payload, ensure_ascii=False)-- # steer / stop need a resolvable, owned target.- sid = (subagent_id or "").strip()- if not sid:- return tool_error(- f"action='{action}' requires subagent_id (from the spawn dispatch "- "response or action='list')."- )- with _active_subagents_lock:- record = _active_subagents.get(sid)- if record is None or not _owns_subagent_record(record, parent_agent):- return tool_error(- f"No live subagent '{sid}' in this conversation's spawn tree. It "- "may have already finished (its result arrives as a normal "- "completion message). Use action='list' to see live children."- )-- if action == "stop":- if interrupt_subagent(sid):- return json.dumps(- {- "action": "stop",- "subagent_id": sid,- "status": "interrupt_requested",- "note": (- "The subagent stops at its next iteration boundary "- "(in-flight tool calls are asked to cancel). Its "- "partial result still re-enters the conversation as a "- "completion message — do not wait or poll."- ),- },- ensure_ascii=False,- )- return tool_error(- f"Could not interrupt '{sid}' — it likely finished in the last "- "moment. Its result arrives as a normal completion message."- )-- if action == "steer":- text = (message or "").strip()- if not text:- return tool_error(- "action='steer' requires a non-empty 'message' describing the "- "course correction."- )- if steer_subagent(sid, text):- return json.dumps(- {- "action": "steer",- "subagent_id": sid,- "status": "queued",- "note": (- "Steering text queued. The subagent sees it appended "- "to its next tool result — the current tool call is "- "never cut. If the child finishes before a delivery "- "boundary remains, the text is reported back as "- "missed_steer in its completion entry."- ),- },- ensure_ascii=False,- )- return tool_error(- f"Subagent '{sid}' is no longer accepting steering (finishing or "- "already finished). Its result arrives as a normal completion "- "message; re-delegate a follow-up task if more work is needed."- )-- return tool_error(f"Unknown action '{action}'. Use spawn, list, steer, or stop.")---def _extract_output_tail(- result: Dict[str, Any],- *,- max_entries: int = 12,- max_chars: int = 8000,-) -> List[Dict[str, Any]]:- """Pull the last N tool-call results from a child's conversation.-- Powers the overlay's "Output" section — the cc-swarm-parity feature.- We reuse the same messages list the trajectory saver walks, taking- only the tail to keep event payloads small. Each entry is- ``{tool, preview, is_error}``.- """- messages = result.get("messages") if isinstance(result, dict) else None- if not isinstance(messages, list):- return []-- # Walk in reverse to build a tail; stop when we have enough.- tail: List[Dict[str, Any]] = []- pending_call_by_id: Dict[str, str] = {}-- # First pass (forward): build tool_call_id -> tool_name map- for msg in messages:- if not isinstance(msg, dict):- continue- if msg.get("role") == "assistant":- for tc in msg.get("tool_calls") or []:- tc_id = tc.get("id")- fn = tc.get("function") or {}- if tc_id:- pending_call_by_id[tc_id] = str(fn.get("name") or "tool")-- # Second pass (reverse): pick tool results, newest first- for msg in reversed(messages):- if len(tail) >= max_entries:- break- if not isinstance(msg, dict) or msg.get("role") != "tool":- continue- # Flatten content-block lists/dicts to text so the overlay shows real- # output (not a "[{'type': 'text'...}]" blob) and error detection can- # see markers buried inside content blocks. Crude str() here would- # mislabel a block-wrapped "Error: ..." result as is_error=False.- content = _stringify_tool_content(msg.get("content") or "")- is_error = _looks_like_error_output(content)- tool_name = pending_call_by_id.get(msg.get("tool_call_id") or "", "tool")- # Preserve line structure so the overlay's wrapped scroll region can- # show real output rather than a whitespace-collapsed blob. We still- # cap the payload size to keep events bounded.- preview = content[:max_chars]- tail.append({"tool": tool_name, "preview": preview, "is_error": is_error})-- tail.reverse() # restore chronological order for display- return tail---def _stringify_tool_content(content: Any) -> str:- """Return a stable text representation for tool-result content.-- Most providers store tool results as strings, but some OpenAI-compatible- paths can return content-block lists. Delegate observability must never- crash while summarising a child run just because the transport used blocks.- """- if content is None:- return ""- if isinstance(content, str):- return content- if isinstance(content, list):- parts = []- for item in content:- if isinstance(item, dict):- text = item.get("text")- if isinstance(text, str):- parts.append(text)- else:- parts.append(json.dumps(item, ensure_ascii=False, default=str))- else:- parts.append(str(item))- return "\n".join(parts)- if isinstance(content, dict):- return json.dumps(content, ensure_ascii=False, default=str)- return str(content)---_TOOL_INPUT_TARGET_KEYS = frozenset({- "cwd",- "destination_path",- "directory",- "dst",- "endpoint",- "file_path",- "new_path",- "old_path",- "path",- "source_path",- "src",- "target_path",- "url",- "urls",-})-_TOOL_INPUT_URL_KEYS = frozenset({"endpoint", "url", "urls"})---def _sanitize_tool_target(key: str, value: Any) -> Any:- """Keep bounded side-effect targets while dropping URL secrets."""- if isinstance(value, list):- cleaned = [- item for item in (_sanitize_tool_target(key, item) for item in value[:16])- if item is not None- ]- return cleaned or None- if not isinstance(value, str) or not value:- return None- bounded = value[:1024]- if key in _TOOL_INPUT_URL_KEYS:- try:- parsed = urlsplit(bounded)- if parsed.scheme and parsed.netloc:- hostname = parsed.hostname- if not hostname:- return None- # ``SplitResult.netloc`` includes ``user:password@``. Rebuild- # the authority from parsed host/port so hook-visible history- # cannot carry URL credentials. Bracket IPv6 literals before- # appending a validated port.- host = f"[{hostname}]" if ":" in hostname else hostname- port = parsed.port- netloc = f"{host}:{port}" if port is not None else host- return urlunsplit((parsed.scheme, netloc, parsed.path, "", ""))- except ValueError:- return None- return bounded---def _summarize_tool_arguments(arguments: Any) -> Dict[str, Any]:- """Summarize argument names and side-effect targets without raw payloads."""- if not isinstance(arguments, str):- return {"argument_keys": [], "targets": {}}- try:- parsed = json.loads(arguments)- except (TypeError, ValueError):- return {"argument_keys": [], "targets": {}}- if not isinstance(parsed, dict):- return {"argument_keys": [], "targets": {}}-- keys = sorted(str(key)[:128] for key in parsed)[:64]- targets: Dict[str, Any] = {}- for raw_key, value in parsed.items():- key = str(raw_key).lower()- if key not in _TOOL_INPUT_TARGET_KEYS:- continue- cleaned = _sanitize_tool_target(key, value)- if cleaned is not None:- targets[key] = cleaned- return {"argument_keys": keys, "targets": targets}---def _sanitize_tool_input_summary(summary: Any) -> Dict[str, Any]:- if not isinstance(summary, dict):- return {"argument_keys": [], "targets": {}}- keys = summary.get("argument_keys")- safe_keys = (- [str(key)[:128] for key in keys[:64]]- if isinstance(keys, list)- else []- )- targets = summary.get("targets")- safe_targets: Dict[str, Any] = {}- if isinstance(targets, dict):- for raw_key, value in targets.items():- key = str(raw_key).lower()- if key not in _TOOL_INPUT_TARGET_KEYS:- continue- cleaned = _sanitize_tool_target(key, value)- if cleaned is not None:- safe_targets[key] = cleaned- return {"argument_keys": safe_keys, "targets": safe_targets}---def _subagent_stop_tool_call_history(tool_trace: Any) -> List[Dict[str, Any]]:- """Build a detached, metadata-only tool history for lifecycle hooks."""- if not isinstance(tool_trace, list):- return []-- history: List[Dict[str, Any]] = []- for item in tool_trace:- if not isinstance(item, dict):- continue- tool_name = str(item.get("tool") or "unknown")[:256]- status = str(item.get("status") or "unknown").lower()- if status not in {"ok", "error"}:- status = "unknown"-- def _byte_count(key: str) -> int:- value = item.get(key, 0)- if not isinstance(value, (int, float)) or isinstance(value, bool):- return 0- return max(0, int(value))-- history.append({- "tool_name": tool_name,- "tool_input": _sanitize_tool_input_summary(item.get("input_summary")),- "input_bytes": _byte_count("args_bytes"),- "output_bytes": _byte_count("result_bytes"),- "status": status,- })- return history---def _looks_like_error_output(content: Any) -> bool:- """Conservative stderr/error detector for tool-result previews.-- The old heuristic flagged any preview containing the substring "error",- which painted perfectly normal terminal/json output red. We now only- mark output as an error when there is stronger evidence:- - structured JSON with an ``error`` key- - structured JSON with ``status`` of error/failed- - first line starts with a classic error marker- """- content = _stringify_tool_content(content)- if not content:- return False-- head = content.lstrip()- if head.startswith("{") or head.startswith("["):- try:- parsed = json.loads(content)- if isinstance(parsed, dict):- if parsed.get("error"):- return True- status = str(parsed.get("status") or "").strip().lower()- if status in {"error", "failed", "failure", "timeout"}:- return True- except Exception:- pass-- first = content.splitlines()[0].strip().lower() if content.splitlines() else ""- return (- first.startswith("error:")- or first.startswith("failed:")- or first.startswith("traceback ")- or first.startswith("exception:")- )+# Nested delegation is granted by depth/role in _build_child_agent, never by the+# model naming toolsets (there is no model-facing toolsets argument).def _normalize_role(r: Optional[str]) -> str:- """Normalise a caller-provided role to 'leaf' or 'orchestrator'.-- None/empty -> 'leaf'. Unknown strings coerce to 'leaf' with a- warning log (matches the silent-degrade pattern of- _get_orchestrator_enabled). _build_child_agent adds a second- degrade layer for depth/kill-switch bounds.- """+ """'leaf' | 'orchestrator'; None/empty/unknown -> 'leaf' (unknown warns)."""if r is None or not r:return "leaf"r_norm = str(r).strip().lower()@@ -900,195 +140,6 @@ def _normalize_role(r: Optional[str]) -> str:return "leaf"-def _get_max_concurrent_children() -> int:- """Read delegation.max_concurrent_children from config, falling back to- DELEGATION_MAX_CONCURRENT_CHILDREN env var, then the default (10).-- Users can raise this as high as they want; only the floor (1) is enforced.-- Uses the same ``_load_config()`` path that the rest of ``delegate_task``- uses, keeping config priority consistent (config.yaml > env > default).- """- cfg = _load_config()- val = cfg.get("max_concurrent_children")- if val is not None:- try:- result = max(1, int(val))- if result > 10:- global _HIGH_CONCURRENCY_WARNED- if not _HIGH_CONCURRENCY_WARNED:- _HIGH_CONCURRENCY_WARNED = True- logger.warning(- "delegation.max_concurrent_children=%d: each child consumes API tokens "- "independently. High values multiply cost linearly.",- result,- )- return result- except (TypeError, ValueError):- logger.warning(- "delegation.max_concurrent_children=%r is not a valid integer; "- "using default %d",- val,- _DEFAULT_MAX_CONCURRENT_CHILDREN,- )- return _DEFAULT_MAX_CONCURRENT_CHILDREN- env_val = os.getenv("DELEGATION_MAX_CONCURRENT_CHILDREN")- if env_val:- try:- return max(1, int(env_val))- except (TypeError, ValueError):- return _DEFAULT_MAX_CONCURRENT_CHILDREN- return _DEFAULT_MAX_CONCURRENT_CHILDREN---def _get_worktree_isolation() -> bool:- """Read delegation.worktree_isolation from config (bool, default False).-- Inspired by Muse Code's ``--subagent-worktree-isolation`` (Meta, Aug- 2026): when enabled, each delegated child gets its own git worktree- checked out from the parent's current commit so parallel children never- contend for the same working copy. Opt-in and git-only — in a non-git- workspace or on a non-local terminal backend the flag is ignored without- an error and children share the parent's workspace as before.- """- cfg = _load_config()- return bool(cfg.get("worktree_isolation", False))---_LEGACY_MAX_ASYNC_WARNED = False---def _get_max_async_children() -> int:- """Concurrency cap for background (``background=true``) delegations.-- DEPRECATED KNOB: ``delegation.max_async_children`` has been unified into- ``delegation.max_concurrent_children`` — one cap governs both a single- synchronous batch's parallelism and how many background delegation units- may run at once. When at capacity, a new async dispatch is REJECTED (not- queued) so a runaway model can't pile up unbounded background work; the- caller falls back to running the work synchronously.-- A leftover ``max_async_children`` in config.yaml is ignored (the config- migration removes it, folding a raised value into- ``max_concurrent_children``); we log a one-time deprecation warning if- one is still present.- """- global _LEGACY_MAX_ASYNC_WARNED- cfg = _load_config()- if cfg.get("max_async_children") is not None and not _LEGACY_MAX_ASYNC_WARNED:- _LEGACY_MAX_ASYNC_WARNED = True- logger.warning(- "delegation.max_async_children is deprecated and ignored; "- "delegation.max_concurrent_children now caps background "- "delegations too. Remove the stale key from config.yaml."- )- return _get_max_concurrent_children()---def _get_child_timeout() -> Optional[float]:- """Read delegation.child_timeout_seconds from config.-- Returns the number of seconds a single child agent is allowed to run- before being cut off, or ``None`` when no wall-clock cap applies.-- Default: ``None`` (no timeout). Subagents doing legitimate heavy work- (deep code review, large research fan-outs, slow reasoning models) were- routinely killed mid-task by the old blanket cap even though they were- making steady progress. Failures should come from what the child is- actually doing — API errors, tool errors, iteration budget — not from a- generic delegation-level stopwatch. Stuck-child protection is handled- separately by the heartbeat staleness monitor, which stops refreshing- parent activity so the gateway inactivity timeout can fire.-- Set ``delegation.child_timeout_seconds`` to a positive number to opt back- in to a hard cap (floor 30 s); ``0`` or a negative value means disabled.- """- cfg = _load_config()- val = cfg.get("child_timeout_seconds")- if val is not None:- try:- parsed = float(val)- except (TypeError, ValueError):- logger.warning(- "delegation.child_timeout_seconds=%r is not a valid number; "- "using default (no timeout)",- val,- )- else:- return None if parsed <= 0 else max(30.0, parsed)- env_val = os.getenv("DELEGATION_CHILD_TIMEOUT_SECONDS")- if env_val:- try:- parsed = float(env_val)- except (TypeError, ValueError):- pass- else:- return None if parsed <= 0 else max(30.0, parsed)- return DEFAULT_CHILD_TIMEOUT---def _get_max_spawn_depth() -> int:- """Read delegation.max_spawn_depth from config, floored at 1 (no ceiling).-- depth 0 = parent agent. max_spawn_depth = N means agents at depths- 0..N-1 can spawn; depth N is the leaf floor. Default 1 is flat:- parent spawns children (depth 1), depth-1 children cannot spawn- (blocked by this guard AND, for leaf children, by the delegation- toolset strip in _strip_blocked_tools).-- Raise to 2+ to unlock nested orchestration. role="orchestrator"- removes the toolset strip for spawning children when- max_spawn_depth >= 2, enabling them to spawn their own workers.- Like max_concurrent_children, there is no upper ceiling — but each- extra level multiplies API cost, so raise it deliberately.- """- cfg = _load_config()- val = cfg.get("max_spawn_depth")- if val is None:- return MAX_DEPTH- try:- ival = int(val)- except (TypeError, ValueError):- logger.warning(- "delegation.max_spawn_depth=%r is not a valid integer; " "using default %d",- val,- MAX_DEPTH,- )- return MAX_DEPTH- floored = max(_MIN_SPAWN_DEPTH, ival)- if floored != ival:- logger.warning(- "delegation.max_spawn_depth=%d below floor %d; using %d",- ival,- _MIN_SPAWN_DEPTH,- floored,- )- return floored---def _get_orchestrator_enabled() -> bool:- """Global kill switch for the orchestrator role.-- When False, role="orchestrator" is silently forced to "leaf" in- _build_child_agent and the delegation toolset is stripped as before.- Lets an operator disable the feature without a code revert.- """- cfg = _load_config()- val = cfg.get("orchestrator_enabled", True)- if isinstance(val, bool):- return val- # Accept "true"/"false" strings from YAML that doesn't auto-coerce.- if isinstance(val, str):- return val.strip().lower() in {"true", "1", "yes", "on"}- return True---def _get_inherit_mcp_toolsets() -> bool:- """Whether narrowed child toolsets should keep the parent's MCP toolsets."""- cfg = _load_config()- return is_truthy_value(cfg.get("inherit_mcp_toolsets"), default=True)--def _is_mcp_toolset_name(name: str) -> bool:"""Return True for canonical MCP toolsets and their registered aliases."""if not name:@@ -1105,16 +156,10 @@ def _is_mcp_toolset_name(name: str) -> bool:def _expand_parent_toolsets(parent_toolsets: set) -> set:- """Expand composite toolsets so individual toolset names are recognized.-- When a parent uses a composite toolset like ``hermes-cli`` (which bundles- all core tools), the child may request individual toolsets such as ``web``- or ``terminal``. A simple name-based intersection would reject them- because ``"web" != "hermes-cli"``.+ """Add every toolset whose tools are a subset of the parent's tools.- This helper collects the tool names from each parent toolset, then adds- the names of any individual toolsets whose tools are a *subset* of the- parent's available tools. The original parent toolset names are preserved.+ A parent on a composite like ``hermes-cli`` must still let a child request+ ``web``/``terminal``; bare name intersection would reject them."""parent_tool_names: set = set()for ts_name in parent_toolsets:@@ -1147,229 +192,25 @@ def _preserve_parent_mcp_toolsets(DEFAULT_MAX_ITERATIONS = 250-# Hard per-summary character ceiling layered on top of the dynamic-# headroom budget (see _apply_summary_budget). Belt-and-suspenders for-# models that ignore the "be concise" instruction. 0 disables the ceiling.-DEFAULT_MAX_SUMMARY_CHARS = 24000-# Fraction of the parent's *remaining* context headroom that the whole batch-# of subagent summaries is allowed to consume. The per-summary budget is this-# slice divided across the batch, so N children can't collectively blow the-# parent's window (the compression/429 death-spiral in issue/PR #9126).-_SUMMARY_HEADROOM_FRACTION = 0.5-# Floor so a single summary always gets a usable slice even when the parent is-# already nearly full — below this we'd be truncating to noise.-_MIN_SUMMARY_CHARS = 2000-# No default wall-clock cap on child agents: legitimate heavy subagent work-# (deep reviews, research fan-outs, slow reasoning models) was being killed-# mid-task. Errors should come from what the child actually does; stuck-child-# detection lives in the heartbeat staleness monitor below. Users can opt back-# in via delegation.child_timeout_seconds.-DEFAULT_CHILD_TIMEOUT: Optional[float] = None_HEARTBEAT_INTERVAL = 30 # seconds between parent activity heartbeats during delegation-# Stale-heartbeat thresholds. A child with no observable progress is either:-# - idle between turns (no current_tool, frozen last_activity_ts) — wedged-# - inside a tool (current_tool set) — probably running a legitimately long-# operation (terminal command, web fetch, large file read)-# An in-flight model wait is NOT idle: direct_api_call refreshes-# last_activity_ts while the request is open, and the monitor treats that-# timestamp advance as progress (same signal as streamed chunks / async-# stall monitor). Slow local GGUF / long-prefill models must not be killed-# for taking longer than the idle window on a single completion.-# The idle ceiling stays tight so a child that is truly between turns with-# no activity doesn't mask the gateway timeout. The in-tool ceiling is much-# higher so legit long-running tools get time to finish;-# delegation.child_timeout_seconds (off by default) remains an optional hard-# cap for users who want one.-_HEARTBEAT_STALE_CYCLES_IDLE = 15 # 15 * 30s = 450s idle between turns → stale-_HEARTBEAT_STALE_CYCLES_IN_TOOL = 40 # 40 * 30s = 1200s stuck on same tool → stale+# Stale-heartbeat thresholds (cycles of _HEARTBEAT_INTERVAL with no progress).+# Progress = iteration, current_tool OR last_activity_ts advancing; an in-flight+# model wait refreshes last_activity_ts, so slow models are not "idle". Idle+# stays tight so a truly wedged child doesn't mask the gateway timeout; in-tool+# is much higher so legitimately long tools can finish.+_HEARTBEAT_STALE_CYCLES_IDLE = 15 # 450s idle between turns → stale+_HEARTBEAT_STALE_CYCLES_IN_TOOL = 40 # 1200s stuck on same tool → staleDEFAULT_TOOLSETS = ["terminal", "file", "web"]-# ----------------------------------------------------------------------------# Delegation progress event types-# ------------------------------------------------------------------------------class DelegateEvent(str, enum.Enum):- """Formal event types emitted during delegation progress.-- _build_child_progress_callback normalises incoming legacy strings- (``tool.started``, ``_thinking``, …) to these enum values via- ``_LEGACY_EVENT_MAP``. External consumers (gateway SSE, ACP adapter,- CLI) still receive the legacy strings during the deprecation window.-- TASK_SPAWNED / TASK_COMPLETED / TASK_FAILED are reserved for- future orchestrator lifecycle events and are not currently emitted.- """-- TASK_SPAWNED = "delegate.task_spawned"- TASK_PROGRESS = "delegate.task_progress"- TASK_COMPLETED = "delegate.task_completed"- TASK_FAILED = "delegate.task_failed"- TASK_THINKING = "delegate.task_thinking"- TASK_TOOL_STARTED = "delegate.tool_started"- TASK_TOOL_COMPLETED = "delegate.tool_completed"---# Legacy event strings → DelegateEvent mapping.-# Incoming child-agent events use the old names; the callback normalises them.-_LEGACY_EVENT_MAP: Dict[str, DelegateEvent] = {- "_thinking": DelegateEvent.TASK_THINKING,- "reasoning.available": DelegateEvent.TASK_THINKING,- "tool.started": DelegateEvent.TASK_TOOL_STARTED,- "tool.completed": DelegateEvent.TASK_TOOL_COMPLETED,- "subagent_progress": DelegateEvent.TASK_PROGRESS,-}--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,- *,- workspace_path: Optional[str] = None,- role: str = "leaf",- max_spawn_depth: int = 2,- child_depth: int = 1,-) -> str:- """Build a focused system prompt for a child agent.-- When role='orchestrator', appends a delegation-capability block- modeled on OpenClaw's buildSubagentSystemPrompt (canSpawn branch at- inspiration/openclaw/src/agents/subagent-system-prompt.ts:63-95).- The depth note is literal truth (grounded in the passed config) so- the LLM doesn't confabulate nesting capabilities that don't exist.- """- 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}")- if workspace_path and str(workspace_path).strip():- parts.append(- "\nWORKSPACE PATH:\n"- f"{workspace_path}\n"- "Use this exact path for local repository/workdir operations unless the task explicitly says otherwise."- )- # Project context files (AGENTS.md / CLAUDE.md / .cursorrules ...)- # from the workspace, via the SAME discovery/priority/cap logic the- # main agent's system prompt uses. Children are constructed with- # skip_context_files=True (their prompt is this focused one), so- # without this a subagent works in a repo without the repo's own- # conventions unless it thinks to go read them. SOUL.md is skipped —- # identity belongs to the parent. workspace_path comes only from- # explicit sources (_resolve_workspace_hint: TERMINAL_CWD / agent cwd- # hints, never bare getcwd), so the #64590 install-tree-fallback leak- # doesn't apply here. Best-effort: on any failure the child prompt is- # simply built without the block.- try:- from agent.prompt_builder import build_context_files_prompt-- _ctx_files = build_context_files_prompt(- cwd=str(workspace_path), skip_soul=True- )- except Exception:- logger.debug(- "subagent: workspace context-files load failed", exc_info=True- )- _ctx_files = ""- if _ctx_files.strip():- parts.append(- "\nThe workspace's project context files are reproduced "- "below. Their conventions and invariants are binding for "- "your work in this workspace.\n\n" + _ctx_files.strip()- )- 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"- "Important workspace rule: Never assume a repository lives at /workspace/... or any other container-style path unless the task/context explicitly gives that path. "- "If no exact local path is provided, discover it first before issuing git/workdir-specific commands.\n\n"- "Keep your final summary tight: lead with outcomes, prefer bullet "- "points over paragraphs, and don't replay your whole process. Your "- "response is returned to the parent agent as a summary, and overlong "- "summaries crowd out the parent's context window."- )- if role == "orchestrator":- child_note = (- "Your own children MUST be leaves (cannot delegate further) "- "because they would be at the depth floor — you cannot pass "- "role='orchestrator' to your own delegate_task calls."- if child_depth + 1 >= max_spawn_depth- else "Your own children can themselves be orchestrators or leaves, "- "depending on the `role` you pass to delegate_task. Default is "- "'leaf'; pass role='orchestrator' explicitly when a child "- "needs to further decompose its work."- )- parts.append(- "\n## Subagent Spawning (Orchestrator Role)\n"- "You have access to the `delegate_task` tool and CAN spawn "- "your own subagents to parallelize independent work.\n\n"- "WHEN to delegate:\n"- "- The goal decomposes into 2+ independent subtasks that can "- "run in parallel (e.g. research A and B simultaneously).\n"- "- A subtask is reasoning-heavy and would flood your context "- "with intermediate data.\n\n"- "WHEN NOT to delegate:\n"- "- Single-step mechanical work — do it directly.\n"- "- Trivial tasks you can execute in one or two tool calls.\n"- "- Re-delegating your entire assigned goal to one worker "- "(that's just pass-through with no value added).\n\n"- "Coordinate your workers' results and synthesize them before "- "reporting back to your parent. You are responsible for the "- "final summary, not your workers.\n\n"- f"NOTE: You are at depth {child_depth}. The delegation tree "- f"is capped at max_spawn_depth={max_spawn_depth}. {child_note}"- )- return "\n".join(parts)---def _resolve_workspace_hint(parent_agent) -> Optional[str]:- """Best-effort local workspace hint for child prompts.-- We only inject a path when we have a concrete absolute directory. This avoids- teaching subagents a fake container path while still helping them avoid- guessing `/workspace/...` for local repo tasks.- """- candidates = [- os.getenv("TERMINAL_CWD"),- getattr(- getattr(parent_agent, "_subdirectory_hints", None), "working_dir", None- ),- getattr(parent_agent, "terminal_cwd", None),- getattr(parent_agent, "cwd", None),- ]- for candidate in candidates:- if not candidate:- continue- try:- text = os.path.abspath(os.path.expanduser(str(candidate)))- except Exception:- continue- if os.path.isabs(text) and os.path.isdir(text):- return text- return None--def _strip_blocked_tools(toolsets: List[str]) -> List[str]:- """Remove toolsets that contain only blocked tools.-- The strip set is derived from DELEGATE_BLOCKED_TOOLS plus the explicit- composite/scenario toolsets (delegation, code_execution) that have no- one-to-one tool. This keeps the blocklist and the strip set in lockstep- so new blocked tools can't silently leak through as toolset names.- """- # Composite toolsets that should never pass through to children, even- # though their individual tools aren't all in DELEGATE_BLOCKED_TOOLS.+ """Remove toolsets whose tools are ALL blocked (derived from DELEGATE_BLOCKED_TOOLS+ so the two can't drift) plus composite toolsets children must never get."""_COMPOSITE_BLOCKED_TOOLSETS = frozenset({"delegation"})blocked_toolset_names = {name@@ -1381,429 +222,35 @@ def _strip_blocked_tools(toolsets: List[str]) -> List[str]:return [t for t in toolsets if t not in blocked_toolset_names]-def _blocked_toolsets_for_role(role: str) -> List[str]:- """Return one-tool deny toolsets for a delegated child role.-- ``_strip_blocked_tools`` can remove fully blocked toolsets, but it must keep- mixed platform bundles such as ``hermes-cli`` because those also contain- useful tools. Passing these exact deny toolsets to AIAgent lets- ``model_tools`` subtract blocked names *after* composite expansion, and the- restriction survives later registry/MCP refreshes through the agent's- stored ``disabled_toolsets``.- """- blocked_names = set(DELEGATE_BLOCKED_TOOLS)- if role == "orchestrator":- blocked_names.discard("delegate_task")- return sorted(- name- for name, defn in TOOLSETS.items()- if defn.get("tools")- and set(defn.get("tools", ())).issubset(blocked_names)- )---_BATCH_ORDINALS: Dict[str, int] = {}-_BATCH_ORDINALS_LOCK = threading.Lock()---def format_batch_tag(delegation_id: Optional[str]) -> str:- """Short human tag identifying which delegation batch a line belongs to.-- ``deleg_6a664903`` → ``set 1`` (first batch seen in this process),- the next distinct id → ``set 2``, and so on. Several batches (a parent's- fan-out plus a child's nested fan-out, or two concurrent tools) print- interleaved ``[n/N]`` progress lines to the same console; without a batch- tag a ``✓ [3/3]`` and a ``✓ [3/9]`` are indistinguishable, and a raw hex- slice (``[b2ac 3/9]``) is attributable but unreadable. Empty string when- no id is known so callers can concatenate unconditionally.- """- if not isinstance(delegation_id, str) or not delegation_id:- return ""- with _BATCH_ORDINALS_LOCK:- n = _BATCH_ORDINALS.get(delegation_id)- if n is None:- n = len(_BATCH_ORDINALS) + 1- _BATCH_ORDINALS[delegation_id] = n- return f"set {n}"---def _batch_prefix(delegation_id: Optional[str], task_index: int, task_count: int) -> str:- """``[set 2 · 3/9] `` for batch children, ``[set 2] `` for a lone child,- ``[3/9] `` / ``""`` when the batch id is unknown."""- tag = format_batch_tag(delegation_id)- if task_count > 1:- inner = f"{tag} · {task_index + 1}/{task_count}" if tag else f"{task_index + 1}/{task_count}"- return f"[{inner}] "- return f"[{tag}] " if tag else ""---def _emit_parent_console(parent_agent, line: str) -> None:- """Emit a human-readable progress line to the parent's console.-- Routes through ``parent_agent._safe_print`` when available so headless- stdio hosts (ACP, gateway API) can redirect non-protocol output to- stderr via their configured ``_print_fn``. A bare ``print()`` would- otherwise land on stdout and corrupt JSON-RPC framing.- """- printer = getattr(parent_agent, "_safe_print", None)- if callable(printer):- try:- printer(line)- return- except Exception:- pass- print(line)---def _build_child_progress_callback(- task_index: int,- goal: str,- parent_agent,- task_count: int = 1,- *,- subagent_id: Optional[str] = None,- parent_id: Optional[str] = None,- depth: Optional[int] = None,- model: Optional[str] = None,- toolsets: Optional[List[str]] = None,- session_ref: Optional[Dict[str, Any]] = None,-) -> Optional[callable]:- """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-- The identity kwargs (``subagent_id``, ``parent_id``, ``depth``, ``model``,- ``toolsets``) are threaded into every relayed event so the TUI can- reconstruct the live spawn tree and route per-branch controls (kill,- pause) back by ``subagent_id``. All are optional for backward compat —- older callers that ignore them still produce a flat list on the TUI.-- 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-- # Show 1-indexed prefix only in batch mode (multiple tasks). The batch tag- # (short delegation id) is resolved lazily from session_ref because the- # callback is built before delegate_task stamps ``_delegation_id`` on the- # child; delegate_task drops the id into the same shared ref.- def _prefix() -> str:- deleg = session_ref.get("delegation_id") if session_ref else None- return _batch_prefix(deleg, task_index, task_count)-- goal_label = (goal or "").strip()-- # Gateway: batch tool names, flush periodically- _BATCH_SIZE = 5- _batch: List[str] = []- _tool_count = [0] # per-subagent running counter (list for closure mutation)-- def _identity_kwargs() -> Dict[str, Any]:- kw: Dict[str, Any] = {- "task_index": task_index,- "task_count": task_count,- "goal": goal_label,- }- if subagent_id is not None:- kw["subagent_id"] = subagent_id- if parent_id is not None:- kw["parent_id"] = parent_id- if depth is not None:- kw["depth"] = depth- if model is not None:- kw["model"] = model- if toolsets is not None:- kw["toolsets"] = list(toolsets)- # The child's own session id — filled into the shared ref once the- # child agent exists (the callback is built first), so every relayed- # event lets UIs open/inspect the subagent's session directly.- if session_ref and session_ref.get("session_id"):- kw["child_session_id"] = str(session_ref["session_id"])- if session_ref and session_ref.get("delegation_id"):- kw["delegation_id"] = str(session_ref["delegation_id"])- kw["tool_count"] = _tool_count[0]- return kw-- def _relay(- event_type: str, tool_name: str = None, preview: str = None, args=None, **kwargs- ):- if not parent_cb:- return- payload = _identity_kwargs()- payload.update(kwargs) # caller overrides (e.g. status, duration_seconds)- try:- parent_cb(event_type, tool_name, preview, args, **payload)- except Exception as e:- logger.debug("Parent callback failed: %s", e)-- def _callback(- event_type, tool_name: str = None, preview: str = None, args=None, **kwargs- ):- # Lifecycle events emitted by the orchestrator itself — handled- # before enum normalisation since they are not part of DelegateEvent.- if event_type == "subagent.start":- if spinner and goal_label:- short = (- (goal_label[:55] + "...") if len(goal_label) > 55 else goal_label- )- try:- spinner.print_above(f" {_prefix()}├─ 🔀 {short}")- except Exception as e:- logger.debug("Spinner print_above failed: %s", e)- _relay("subagent.start", preview=preview or goal_label or "", **kwargs)- return-- if event_type == "subagent.complete":- # Failed child: echo one clean reason line into the CLI tree so- # the human sees WHY, not just a vanished branch. Gateway-side- # rendering happens in TurnRunner.progress_callback off the- # relayed event below.- if spinner and kwargs.get("status") in SUBAGENT_FAILURE_STATUSES:- _fail_line = format_subagent_failure_line(- goal_label,- kwargs.get("status"),- error=kwargs.get("summary") or preview,- duration_seconds=kwargs.get("duration_seconds"),- )- try:- spinner.print_above(f" {_prefix()}├─ {_fail_line}")- except Exception as e:- logger.debug("Spinner print_above failed: %s", e)- _relay("subagent.complete", preview=preview, **kwargs)- return-- if event_type == "subagent.text":- # Streamed assistant reply text from the child. Relay verbatim so a- # gateway watch window can mirror the child "talking" as it streams.- # No spinner echo — the CLI shows the child via the tree, and the- # CLI/TUI progress handlers ignore non-tool event types, so this is- # inert there; only a gateway watch window consumes it.- _relay("subagent.text", preview=preview)- return-- # Normalise legacy strings, new-style "delegate.*" strings, and- # DelegateEvent enum values all to a single DelegateEvent. The- # original implementation only accepted the five legacy strings;- # enum-typed callers were silently dropped.- if isinstance(event_type, DelegateEvent):- event = event_type- else:- event = _LEGACY_EVENT_MAP.get(event_type)- if event is None:- try:- event = DelegateEvent(event_type)- except (ValueError, TypeError):- return # Unknown event — ignore-- if event == DelegateEvent.TASK_THINKING:- text = preview or tool_name or ""- if spinner:- short = (text[:55] + "...") if len(text) > 55 else text- try:- spinner.print_above(f' {_prefix()}├─ 💭 "{short}"')- except Exception as e:- logger.debug("Spinner print_above failed: %s", e)- _relay("subagent.thinking", preview=text)- return-- if event == DelegateEvent.TASK_TOOL_COMPLETED:- return-- if event == DelegateEvent.TASK_PROGRESS:- # Pre-batched progress summary relayed from a nested- # orchestrator's grandchild (upstream emits as- # parent_cb("subagent_progress", summary_string) where the- # summary lands in the tool_name positional slot). Treat as- # a pass-through: render distinctly (not via the tool-start- # emoji lookup, which would mistake the summary string for a- # tool name) and relay upward without re-batching.- summary_text = tool_name or preview or ""- if spinner and summary_text:- try:- spinner.print_above(f" {_prefix()}├─ 🔀 {summary_text}")- except Exception as e:- logger.debug("Spinner print_above failed: %s", e)- if parent_cb:- try:- parent_cb("subagent_progress", f"{_prefix()}{summary_text}")- except Exception as e:- logger.debug("Parent callback relay failed: %s", e)- return-- # TASK_TOOL_STARTED — display and batch for parent relay- _tool_count[0] += 1- if subagent_id is not None:- with _active_subagents_lock:- rec = _active_subagents.get(subagent_id)- if rec is not None:- rec["tool_count"] = _tool_count[0]- rec["last_tool"] = tool_name or ""- if spinner:- short = (- (preview[:35] + "...")- if preview and len(preview) > 35- else (preview or "")- )- from agent.display import get_tool_emoji-- emoji = get_tool_emoji(tool_name or "")- line = f" {_prefix()}├─ {emoji} {tool_name}"- if short:- line += f' "{short}"'- try:- spinner.print_above(line)- except Exception as e:- logger.debug("Spinner print_above failed: %s", e)-- if parent_cb:- _relay("subagent.tool", tool_name, preview, args)- _batch.append(tool_name or "")- if len(_batch) >= _BATCH_SIZE:- summary = ", ".join(_batch)- _relay("subagent.progress", preview=f"🔀 {_prefix()}{summary}")- _batch.clear()-- def _flush():- """Flush remaining batched tool names to gateway on completion."""- if parent_cb and _batch:- summary = ", ".join(_batch)- _relay("subagent.progress", preview=f"🔀 {_prefix()}{summary}")- _batch.clear()-- _callback._flush = _flush- return _callback---def _normalized_runtime_url(value: Any) -> str:- return str(value or "").strip().rstrip("/")---def _inherit_parent_capabilities(- parent_agent, override_provider, override_base_url-) -> Optional[dict]:- """Return the parent's endpoint-trust capability map for a child, or None.-- The trusted-proxy capability map (``agent.capabilities``, e.g.- ``openai_native_compaction`` from a custom_providers entry) is a trust- decision scoped to one provider+endpoint. A child inherits it ONLY when- it runs against the parent's exact route — any delegation override that- changes provider or base_url stays DEFAULT-DENY, matching the /model- switch posture (#94036/#97292).- """- if override_provider or override_base_url:- return None- parent_caps = getattr(parent_agent, "capabilities", None)- if not isinstance(parent_caps, dict):- return None- return {- key: value- for key, value in parent_caps.items()- if isinstance(key, str) and isinstance(value, bool)- }---def _inherit_parent_base_url(parent_agent, fallback_base_url: Optional[str]) -> Optional[str]:- """Return the base URL the parent is actually calling, not a stale attribute.-- ``parent_agent.base_url`` can still carry a leftover OpenRouter URL from an- old config while the live OpenAI client in ``_client_kwargs`` already points- at local Ollama. Subagents must inherit the active endpoint or they 401- against OpenRouter with a dummy/local key.- """- surface_url = _normalized_runtime_url(fallback_base_url)- client_kwargs = getattr(parent_agent, "_client_kwargs", None)- if isinstance(client_kwargs, dict):- kwargs_url = _normalized_runtime_url(client_kwargs.get("base_url"))- if (- kwargs_url- and kwargs_url != surface_url- and kwargs_url.startswith(("http://", "https://"))- ):- return kwargs_url-- client = getattr(parent_agent, "client", None)- if client is not None:- # OpenAI SDK exposes ``base_url`` as an ``httpx.URL``, not ``str`` —- # coerce so the comparison works regardless of the client's type.- live_url = _normalized_runtime_url(getattr(client, "base_url", ""))- if (- live_url- and live_url != surface_url- and live_url.startswith(("http://", "https://"))- ):- return live_url-- return fallback_base_url or None---def _build_child_agent(- task_index: int,- goal: str,- context: Optional[str],- toolsets: Optional[List[str]],- model: Optional[str],- max_iterations: int,- task_count: int,- parent_agent,- # 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,- override_request_overrides: Optional[Dict[str, Any]] = None,- override_max_tokens: Optional[int] = None,- # ACP transport overrides from trusted delegation config.- override_acp_command: Optional[str] = None,- override_acp_args: Optional[List[str]] = None,- # Per-call role controlling whether the child can further delegate.- # 'leaf' (default) cannot; 'orchestrator' retains the delegation- # toolset subject to depth/kill-switch bounds applied below.- role: str = "leaf",-):- """- Build a child AIAgent on the main thread (thread-safe construction).- Returns the constructed child agent without running it.-- 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).- """- from run_agent import AIAgent- import uuid as _uuid-- # ── Role resolution ─────────────────────────────────────────────────- # Depth-derived, not caller-declared: a child may delegate iff the- # kill switch is on and depth budget remains below max_spawn_depth.- # The legacy `role` arg no longer participates (it asked the caller- # to guess a fact the config already knows); it is still accepted and- # normalised for wire compat, but capability comes from depth alone.- child_depth = getattr(parent_agent, "_delegate_depth", 0) + 1- max_spawn = _get_max_spawn_depth()- orchestrator_ok = _get_orchestrator_enabled() and child_depth < max_spawn- effective_role = "orchestrator" if orchestrator_ok else "leaf"+def _blocked_toolsets_for_role(role: str) -> List[str]:+ """One-tool deny toolsets for the role; passed as ``disabled_toolsets`` so+ blocked names inside mixed bundles are subtracted AFTER composite expansion."""+ blocked_names = set(DELEGATE_BLOCKED_TOOLS)+ if role == "orchestrator":+ blocked_names.discard("delegate_task")+ return sorted(+ name+ for name, defn in TOOLSETS.items()+ if defn.get("tools")+ and set(defn.get("tools", ())).issubset(blocked_names)+ )- # ── Subagent identity (stable across events, 0-indexed for TUI) ─────- # subagent_id is generated here so the progress callback, the- # spawn_requested event, and the _active_subagents registry all share- # one key. parent_id is non-None when THIS parent is itself a subagent- # (nested orchestrator -> worker chain).- subagent_id = f"sa-{task_index}-{_uuid.uuid4().hex[:8]}"- parent_subagent_id = getattr(parent_agent, "_subagent_id", None)- tui_depth = max(0, child_depth - 1) # 0 = first-level child for the UI- delegation_cfg = _load_config()+def _resolve_child_toolsets(+ parent_agent, toolsets: Optional[List[str]], effective_role: str+) -> tuple[List[str], List[str]]:+ """Return ``(enabled_toolsets, disabled_toolsets)`` for a child.- # When no explicit toolsets given, inherit from parent's enabled toolsets- # so disabled tools (e.g. web) don't leak to subagents.- # Note: enabled_toolsets=None means "all tools enabled" (the default),- # so we must derive effective toolsets from the parent's loaded tools.+ Children never gain tools the parent lacks: explicit ``toolsets`` are+ intersected with the parent's (composite-expanded) set, otherwise the+ parent's enabled set is inherited. Blocked tools are stripped twice — whole+ blocked toolsets here, and exact one-tool deny toolsets via+ ``disabled_toolsets`` so blocked names inside mixed bundles (hermes-cli)+ are subtracted AFTER composite expansion and survive registry refreshes.+ Orchestrators get ``delegation`` re-added unconditionally (role-granted,+ not inherited).+ """+ # enabled_toolsets=None means "all tools", so derive from loaded tool names.parent_enabled = getattr(parent_agent, "enabled_toolsets", None)if parent_enabled is not None:parent_toolsets = set(parent_enabled)@@ -1820,9 +267,6 @@ def _build_child_agent(parent_toolsets = set(DEFAULT_TOOLSETS)if toolsets:- # Intersect with parent — subagent must not gain tools the parent lacks.- # Expand composite toolsets (e.g. hermes-cli) so that individual- # toolset names (e.g. web, terminal) are recognised during intersection.expanded_parent = _expand_parent_toolsets(parent_toolsets)child_toolsets = [t for t in toolsets if t in expanded_parent]if _get_inherit_mcp_toolsets():@@ -1837,19 +281,12 @@ def _build_child_agent(else:child_toolsets = _strip_blocked_tools(DEFAULT_TOOLSETS)- # Blocked tools also live inside mixed platform bundles (hermes-cli,- # hermes-telegram, etc.) that _strip_blocked_tools must keep because they- # carry useful tools too. Pass exact one-tool deny toolsets through to the- # child so model_tools subtracts the blocked names AFTER composite- # expansion, and the restriction survives later registry/MCP refreshes.raw_parent_disabled = getattr(parent_agent, "disabled_toolsets", None)if isinstance(raw_parent_disabled, (list, tuple, set)):inherited_disabled = [str(name) for name in raw_parent_disabled]else:inherited_disabled = []if effective_role == "orchestrator":- # Role grants delegate_task explicitly, matching the unconditional- # delegation toolset re-add below.inherited_disabled = [name for name in inherited_disabled if name != "delegation"]@@ -1859,93 +296,70 @@ def _build_child_agent())- # Orchestrators retain the 'delegation' toolset that _strip_blocked_tools- # removed. The re-add is unconditional on parent-toolset membership because- # orchestrator capability is granted by role, not inherited — see the- # test_intersection_preserves_delegation_bound test for the design rationale.if effective_role == "orchestrator" and "delegation" not in child_toolsets:child_toolsets.append("delegation")-- workspace_hint = _resolve_workspace_hint(parent_agent)- child_prompt = _build_child_system_prompt(- goal,- context,- workspace_path=workspace_hint,- role=effective_role,- max_spawn_depth=max_spawn,- child_depth=child_depth,- )- # 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"):- parent_api_key = parent_agent._client_kwargs.get("api_key")-- # Resolve the child's effective model early so it can ride on every event.- effective_model_for_cb = model or getattr(parent_agent, "model", None)-- # Build progress callback to relay tool calls to parent display.- # Identity kwargs thread the subagent_id through every emitted event so the- # TUI can reconstruct the spawn tree and route per-branch controls.- child_session_ref: Dict[str, Any] = {}- child_progress_cb = _build_child_progress_callback(- task_index,- goal,- parent_agent,- task_count,- subagent_id=subagent_id,- parent_id=parent_subagent_id,- depth=tui_depth,- model=effective_model_for_cb,- toolsets=child_toolsets,- session_ref=child_session_ref,- )-- # Each subagent gets its own iteration budget capped at max_iterations- # (configurable via delegation.max_iterations, default 50). This means- # total iterations across parent + subagents can exceed the parent's- # max_iterations. The user controls the per-subagent cap in config.yaml.-- child_thinking_cb = None- if child_progress_cb:-- def _child_thinking(text: str) -> None:- if not text:- return- try:- child_progress_cb("_thinking", text)- except Exception as e:- logger.debug("Child thinking callback relay failed: %s", e)-- child_thinking_cb = _child_thinking-- # Resolve effective credentials: config override > parent inherit+ return child_toolsets, child_disabled_toolsets+++@dataclass+class _ChildRuntime:+ """Provider/transport/routing settings resolved for one child AIAgent."""++ model: Any+ provider: Any+ base_url: Any+ api_key: Any+ api_mode: Any+ capabilities: Optional[dict]+ acp_command: Any+ acp_args: list+ reasoning: Any+ fallback: Any+ providers_allowed: Any+ providers_ignored: Any+ providers_order: Any+ provider_sort: Any+ provider_require_parameters: Any+ provider_data_collection: Any+ openrouter_min_coding_score: Any+ optional_kwargs: Dict[str, Any]+++def _resolve_child_runtime(+ parent_agent,+ delegation_cfg: dict,+ parent_api_key: Any,+ *,+ model: Optional[str],+ override_provider: Optional[str],+ override_base_url: Optional[str],+ override_api_key: Optional[str],+ override_api_mode: Optional[str],+ override_max_tokens: Optional[int],+ override_acp_command: Optional[str],+ override_acp_args: Optional[List[str]],+) -> _ChildRuntime:+ """Resolve the child's credentials, transport and routing: config override > parent inherit.++ Rules that are easy to break: api_mode is re-derived (not inherited) when+ the child's provider differs from the parent's or is Nous Portal (dual-wire);+ a pinned ``delegation.command`` must exist on PATH or the spawn fails loudly;+ ``override_provider`` clears the parent's ACP transport, fallback chain and+ OpenRouter routing filters so the pinned provider is actually honoured.+ """effective_model = model or parent_agent.modeleffective_provider = override_provider or getattr(parent_agent, "provider", None)effective_base_url = override_base_url or parent_agent.base_urlif not override_base_url:effective_base_url = _inherit_parent_base_url(parent_agent, effective_base_url)effective_api_key = override_api_key or parent_api_key- # Same-class follow-up to #94036/#97292: the trusted-proxy capability map- # (`agent.capabilities`, e.g. ``openai_native_compaction`` from a- # custom_providers entry) is an endpoint-scoped trust decision. Children- # inherit it ONLY when they run against the parent's exact provider and- # base_url — a provider- or endpoint-changing delegation override stays- # DEFAULT-DENY, matching the /model switch posture. Without this, a child- # on the same trusted proxy silently falls back to local summarization.child_capabilities = _inherit_parent_capabilities(parent_agent, override_provider, override_base_url)- # Bug #20558 / PR #20563: api_mode must NOT be inherited when the child uses a- # different provider than the parent — each provider has its own API surface- # (e.g. MiniMax uses anthropic_messages, DeepSeek uses chat_completions).- # Inheriting the parent's mode causes 404 errors when the child routes to the- # wrong endpoint. Derive the mode from the target provider when it differs.- #- # Nous Portal is dual-wire within a single provider: anthropic/* → Messages,- # everything else → chat_completions. Same-provider inheritance would pin a- # child Hermes/Qwen subagent onto the parent's Claude Messages wire (or the- # reverse). agent_init honors an explicit api_mode above its nous branch, so- # re-derive here before construction.+ # api_mode: each provider has its own wire, so a different provider re-derives+ # (None) instead of inheriting (404s otherwise). Nous Portal is dual-wire+ # within one provider (anthropic/* → Messages, else chat_completions), so+ # same-provider inheritance would pin the child on the wrong wire — re-derive._parent_provider = getattr(parent_agent, "provider", None) or ""_effective_provider_norm = (effective_provider or "").strip().lower()if override_api_mode is not None:@@ -1958,12 +372,8 @@ def _build_child_agent(effective_api_mode = None # force re-derivation from provider's defaultselse:effective_api_mode = getattr(parent_agent, "api_mode", None)- # Defensive: validate trusted delegation.command exists on PATH before- # honoring it. An explicitly pinned transport that cannot run must fail- # the spawn loudly (#80450) — silently falling back to the default- # transport would run the child somewhere the user explicitly routed it- # away from. Normally unreachable via delegate_task, which pre-validates- # the command in _resolve_delegation_credentials.+ # A pinned transport that cannot run must fail the spawn loudly, never fall+ # back silently (delegate_task pre-validates; this covers direct callers).if override_acp_command:import shutil as _shutil@@ -1982,27 +392,21 @@ def _build_child_agent(else (getattr(parent_agent, "acp_args", []) or []))- # When override_provider is set (e.g. delegation.provider: minimax-cn),- # the subagent must use direct API calls — not the parent's ACP transport.- # Inheriting acp_command unconditionally causes run_agent.py to initialize- # CopilotACPClient, bypassing override credentials entirely (issue #16816).+ # A pinned provider must use direct API calls; inheriting the parent's ACP+ # transport would bypass the override credentials entirely.if override_provider and not override_acp_command:effective_acp_command = Noneeffective_acp_args = []if override_acp_command:- # If explicitly forcing an ACP transport override, the provider MUST be copilot-acp- # so run_agent.py initializes the CopilotACPClient.+ # Forced ACP transport requires provider copilot-acp for run_agent to init the client.effective_provider = "copilot-acp"effective_api_mode = "chat_completions"- # Resolve reasoning config: delegation override > parent inherit- parent_reasoning = getattr(parent_agent, "reasoning_config", None)- child_reasoning = parent_reasoning+ # Reasoning: delegation.reasoning_effort > parent. Keep the raw value — a+ # YAML ``false`` must disable thinking, not coerce to "" and inherit.+ child_reasoning = getattr(parent_agent, "reasoning_config", None)try:- # Keep the raw value — ``str(x or "")`` would coerce a YAML boolean- # False (``reasoning_effort: false``) to "" and inherit the parent- # instead of disabling thinking for children.delegation_effort = delegation_cfg.get("reasoning_effort")if delegation_effort or delegation_effort is False:from hermes_constants import parse_reasoning_effort@@ -2018,31 +422,18 @@ def _build_child_agent(except Exception as exc:logger.debug("Could not load delegation reasoning_effort: %s", exc)- # Inherit the parent's fallback provider chain so subagents can recover- # from rate-limits and credential exhaustion exactly like the top-level- # agent does. _fallback_chain is a list accepted by AIAgent's- # fallback_model parameter (which handles both list and dict forms).- #- # EXCEPT when the user pinned delegation.provider: an explicit pin means- # "children run on THIS provider". Inheriting the parent chain would let- # a mid-run auth/429 failure silently reroute the quiet-mode child onto- # the parent's fallback models with no surfaced signal (#80450) — the- # same class of silent-drag the override_provider filter-clearing below- # already prevents for OpenRouter routing preferences. Predictability >- # liveness for explicit pins: the pinned child fails loudly instead.+ # Inherit the parent's fallback chain EXCEPT under a pinned provider: a+ # mid-run 429/auth failure must not silently reroute the quiet child onto+ # the parent's fallbacks. Predictability > liveness for explicit pins.parent_fallback = (Noneif override_providerelse (getattr(parent_agent, "_fallback_chain", None) or None))- # Inherit the parent's OpenRouter provider-preference filters by default- # (so subagents routed to the same provider honour the same routing- # constraints). BUT: when `delegation.provider` is set the user is- # explicitly asking the child to run on a different provider, and- # parent-level OpenRouter filters (e.g. `only=["Anthropic"]`) would- # silently force the child back onto the parent's provider. Clear the- # filters in that case so the delegated provider is honoured.+ # OpenRouter routing filters are inherited, but cleared under a pinned+ # provider — parent filters (e.g. only=["Anthropic"]) would silently force+ # the child back onto the parent's provider.child_providers_allowed = getattr(parent_agent, "providers_allowed", None)child_providers_ignored = getattr(parent_agent, "providers_ignored", None)child_providers_order = getattr(parent_agent, "providers_order", None)@@ -2061,9 +452,7 @@ def _build_child_agent(child_provider_sort = Nonechild_provider_require_parameters = Falsechild_provider_data_collection = ""- # Note: openrouter_min_coding_score is model-gated (only emitted on- # openrouter/pareto-code), so we keep it inherited even when the- # provider is overridden — it's a no-op on any other model.+ # openrouter_min_coding_score stays inherited: model-gated, no-op elsewhere.child_max_tokens = (override_max_tokens@@ -2073,21 +462,138 @@ def _build_child_agent(child_optional_kwargs: Dict[str, Any] = {}if isinstance(child_max_tokens, int):child_optional_kwargs["max_tokens"] = child_max_tokens+ return _ChildRuntime(+ model=effective_model,+ provider=effective_provider,+ base_url=effective_base_url,+ api_key=effective_api_key,+ api_mode=effective_api_mode,+ capabilities=child_capabilities,+ acp_command=effective_acp_command,+ acp_args=effective_acp_args,+ reasoning=child_reasoning,+ fallback=parent_fallback,+ providers_allowed=child_providers_allowed,+ providers_ignored=child_providers_ignored,+ providers_order=child_providers_order,+ provider_sort=child_provider_sort,+ provider_require_parameters=child_provider_require_parameters,+ provider_data_collection=child_provider_data_collection,+ openrouter_min_coding_score=child_openrouter_min_coding_score,+ optional_kwargs=child_optional_kwargs,+ )+++def _build_child_agent(+ task_index: int,+ goal: str,+ context: Optional[str],+ toolsets: Optional[List[str]],+ model: Optional[str],+ max_iterations: int,+ task_count: int,+ parent_agent,+ # Credential overrides from delegation config+ override_provider: Optional[str] = None,+ override_base_url: Optional[str] = None,+ override_api_key: Optional[str] = None,+ override_api_mode: Optional[str] = None,+ override_request_overrides: Optional[Dict[str, Any]] = None,+ override_max_tokens: Optional[int] = None,+ # ACP transport overrides from trusted delegation config.+ override_acp_command: Optional[str] = None,+ override_acp_args: Optional[List[str]] = None,+ # Legacy; accepted for wire compat but ignored (capability is depth-derived).+ role: str = "leaf",+):+ """Build (don't run) a child AIAgent on the main thread.++ override_* (from delegation config) replace parent inheritance so children+ can run on a different provider:model pair.+ """+ from run_agent import AIAgent+ import uuid as _uuid++ # Role is depth-derived: a child may delegate iff the kill switch is on and+ # depth budget remains below max_spawn_depth. The `role` arg is ignored.+ child_depth = getattr(parent_agent, "_delegate_depth", 0) + 1+ max_spawn = _get_max_spawn_depth()+ orchestrator_ok = _get_orchestrator_enabled() and child_depth < max_spawn+ effective_role = "orchestrator" if orchestrator_ok else "leaf"++ # One subagent_id shared by the progress callback, spawn_requested event and+ # the live registry; parent_id is set when THIS parent is itself a subagent.+ subagent_id = f"sa-{task_index}-{_uuid.uuid4().hex[:8]}"+ parent_subagent_id = getattr(parent_agent, "_subagent_id", None)+ tui_depth = max(0, child_depth - 1) # 0 = first-level child for the UI++ delegation_cfg = _load_config()++ child_toolsets, child_disabled_toolsets = _resolve_child_toolsets(+ parent_agent, toolsets, effective_role+ )++ workspace_hint = _resolve_workspace_hint(parent_agent)+ child_prompt = _build_child_system_prompt(+ goal,+ context,+ workspace_path=workspace_hint,+ role=effective_role,+ max_spawn_depth=max_spawn,+ child_depth=child_depth,+ )+ parent_api_key = getattr(parent_agent, "api_key", None)+ if (not parent_api_key) and hasattr(parent_agent, "_client_kwargs"):+ parent_api_key = parent_agent._client_kwargs.get("api_key")++ effective_model_for_cb = model or getattr(parent_agent, "model", None)+ child_session_ref: Dict[str, Any] = {}+ child_progress_cb = _build_child_progress_callback(+ task_index,+ goal,+ parent_agent,+ task_count,+ subagent_id=subagent_id,+ parent_id=parent_subagent_id,+ depth=tui_depth,+ model=effective_model_for_cb,+ toolsets=child_toolsets,+ session_ref=child_session_ref,+ )++ child_thinking_cb = None+ if child_progress_cb:++ def _child_thinking(text: str) -> None:+ if not text:+ return+ try:+ child_progress_cb("_thinking", text)+ except Exception as e:+ logger.debug("Child thinking callback relay failed: %s", e)++ child_thinking_cb = _child_thinking- # Each child gets a DEDICATED SessionDB connection instead of the parent's- # live object. The parent's handle is owned by the parent's lifecycle- # (cron run_job's finally block, gateway session end, /new) and can be- # closed while a fire-and-forget background child is still flushing on a- # daemon thread — every subsequent flush then hits the closed handle and- # the child's transcript is silently dropped (#81267). A dedicated handle- # can't be closed out from under the child; it is released by the child's- # own close() via the owned flag set below. It MUST point at the same- # database FILE as the parent's handle: parents can hold non-default- # per-profile handles (tui_gateway opens SessionDB(db_path=<profile>/- # state.db) for non-launch profiles), and a bare SessionDB() would write- # the child's transcript into the launch profile's db, breaking- # parent_session_id lineage and session_search. AsyncSessionDB wrappers- # (gateway) forward .db_path via __getattr__, so this works through them.+ rt = _resolve_child_runtime(+ parent_agent,+ delegation_cfg,+ parent_api_key,+ model=model,+ override_provider=override_provider,+ override_base_url=override_base_url,+ override_api_key=override_api_key,+ override_api_mode=override_api_mode,+ override_max_tokens=override_max_tokens,+ override_acp_command=override_acp_command,+ override_acp_args=override_acp_args,+ )+ effective_provider, effective_base_url = rt.provider, rt.base_url++ # DEDICATED SessionDB handle for the child: the parent's can be closed by+ # its own lifecycle while a background child still flushes (transcript+ # silently dropped). It MUST open the same db FILE as the parent's handle+ # (non-launch profiles), else lineage/session_search break; released by the+ # child's close() via _owns_session_db.child_session_db = Noneparent_session_db = getattr(parent_agent, "_session_db", None)if parent_session_db is not None:@@ -2112,19 +618,19 @@ def _build_child_agent(with delegated_child_context():try:child = AIAgent(- base_url=effective_base_url,- api_key=effective_api_key,- model=effective_model,- provider=effective_provider,- capabilities=child_capabilities,- api_mode=effective_api_mode,- acp_command=effective_acp_command,- acp_args=effective_acp_args,+ base_url=rt.base_url,+ api_key=rt.api_key,+ model=rt.model,+ provider=rt.provider,+ capabilities=rt.capabilities,+ api_mode=rt.api_mode,+ acp_command=rt.acp_command,+ acp_args=rt.acp_args,max_iterations=max_iterations,- reasoning_config=child_reasoning,+ reasoning_config=rt.reasoning,prefill_messages=getattr(parent_agent, "prefill_messages", None),- fallback_model=parent_fallback,+ fallback_model=rt.fallback,enabled_toolsets=child_toolsets,disabled_toolsets=child_disabled_toolsets,quiet_mode=True,@@ -2137,17 +643,15 @@ def _build_child_agent(thinking_callback=child_thinking_cb,session_db=child_session_db,parent_session_id=getattr(parent_agent, "session_id", None),- providers_allowed=child_providers_allowed,- providers_ignored=child_providers_ignored,- providers_order=child_providers_order,- provider_sort=child_provider_sort,- provider_require_parameters=child_provider_require_parameters,- provider_data_collection=child_provider_data_collection,+ providers_allowed=rt.providers_allowed,+ providers_ignored=rt.providers_ignored,+ providers_order=rt.providers_order,+ provider_sort=rt.provider_sort,+ provider_require_parameters=rt.provider_require_parameters,+ provider_data_collection=rt.provider_data_collection,request_overrides=(- # override_request_overrides is honored whenever set —- # including the inherit branch (override_provider=None),- # where _resolve_delegation_credentials already merged- # delegation.request_overrides OVER the parent's values.+ # honored whenever set, incl. the inherit branch where+ # _resolve_delegation_credentials already merged OVER the parent'sdict(override_request_overrides)if override_request_overrides is not Noneelse (@@ -2156,15 +660,13 @@ def _build_child_agent(else dict(getattr(parent_agent, "request_overrides", {}) or {}))),- openrouter_min_coding_score=child_openrouter_min_coding_score,+ openrouter_min_coding_score=rt.openrouter_min_coding_score,tool_progress_callback=child_progress_cb,iteration_budget=None, # fresh budget per subagent- **child_optional_kwargs,+ **rt.optional_kwargs,)except BaseException:- # Construction failed: the dedicated handle has no owner and no- # child close() will ever run — release it here so the sqlite fds- # don't outlive the failed spawn.+ # No child close() will ever run — release the dedicated handle here.if child_session_db is not None:try:from hermes_state import release_or_close@@ -2173,48 +675,29 @@ def _build_child_agent(passraisechild._print_fn = getattr(parent_agent, "_print_fn", None)- # Ownership transfer for the dedicated handle: the child's close() must- # release it (nothing else holds a reference), and no parent teardown can- # close it out from under a background child (#81267).if child_session_db is not None:- child._owns_session_db = True- # Now the child exists, its session id can ride on every relayed event- # (including the spawn_requested below — first emit happens after this).+ child._owns_session_db = True # released by the child's close(), never by the parent+ # Shared ref: session_id now, delegation_id once delegate_task stamps it —+ # both ride on every relayed event (first emit is spawn_requested below).child_session_ref["session_id"] = getattr(child, "session_id", "") or ""- # Same shared ref receives the batch id once delegate_task stamps it, so- # the display prefix and relayed events can tag which batch this is.child._progress_identity_ref = child_session_ref- # Set delegation depth so children can't spawn grandchildrenchild._delegate_depth = child_depth- # Stash the post-degrade role for introspection (leaf if the- # kill switch or depth bounded the caller's requested role).- child._delegate_role = effective_role- # Stash subagent identity for nested-delegation event propagation and- # for _run_single_child / interrupt_subagent to look up by id.+ child._delegate_role = effective_role # post-degrade rolechild._subagent_id = subagent_idchild._parent_subagent_id = parent_subagent_id- child._subagent_goal = goal- child._parent_turn_id = getattr(parent_agent, "_current_turn_id", "") or ""- # Ownership chain for the model-facing control plane (action=list/steer/- # stop): a parent may only control agents whose weakref chain reaches it.- # Weakref so a finished parent can be collected while a detached child- # record briefly lingers in the registry.+ # Ownership chain for action=list/steer/stop; weakref so a finished parent+ # can be collected while a detached child record lingers in the registry.try:child._delegate_parent_ref = weakref.ref(parent_agent)except TypeError:- # Test doubles (MagicMock et al.) may not be weakref-able; control- # actions then simply don't resolve ownership for this child.- child._delegate_parent_ref = None- # Stable sidebar marker: delegate subagent sessions must stay out of- # session pickers even when a parent delete orphans them (parent_session_id- # → NULL). Mirrors /branch's ``_branched_from`` pattern — see- # ``list_sessions_rich`` child-exclusion clause.+ child._delegate_parent_ref = None # non-weakref-able test doubles+ # Sidebar marker: subagent sessions stay out of session pickers even when a+ # parent delete orphans them (mirrors /branch's ``_branched_from``).parent_sid = getattr(parent_agent, "session_id", None)if parent_sid and getattr(child, "_session_init_model_config", None) is not None:child._session_init_model_config["_delegate_from"] = parent_sid- # Share a credential pool with the child when possible so subagents can- # rotate credentials on rate limits instead of getting pinned to one key.+ # Shared pool lets children rotate credentials on rate limits.child_pool = _resolve_child_credential_pool(effective_provider, parent_agent, effective_base_url)@@ -2230,9 +713,7 @@ def _build_child_agent(else:parent_agent._active_children.append(child)- # Announce the spawn immediately — the child may sit in a queue- # for seconds if max_concurrent_children is saturated, so the TUI- # wants a node in the tree before run starts.+ # Announce now: the child may queue for seconds when the pool is saturated.if child_progress_cb:try:child_progress_cb("subagent.spawn_requested", preview=goal)@@ -2257,362 +738,6 @@ def _build_child_agent(return child-def _dump_subagent_timeout_diagnostic(- *,- child: Any,- task_index: int,- timeout_seconds: float,- duration_seconds: float,- worker_thread: Optional[threading.Thread],- goal: str,-) -> Optional[str]:- """Write a structured diagnostic dump for a subagent that timed out- before making any API call.-- See issue #14726: users hit "subagent timed out after 300s with no response"- with zero API calls and no way to inspect what happened. This helper- writes a dedicated log under ``~/.hermes/logs/subagent-<sid>-<ts>.log``- capturing the child's config, system-prompt / tool-schema sizes, activity- tracker snapshot, and the worker thread's Python stack at timeout.-- Returns the absolute path to the diagnostic file, or None on failure.- """- try:- from hermes_constants import get_hermes_home- import datetime as _dt- import sys as _sys- import traceback as _traceback- import threading as _threading-- hermes_home = get_hermes_home()- logs_dir = hermes_home / "logs"- try:- logs_dir.mkdir(parents=True, exist_ok=True)- except Exception:- return None-- subagent_id = getattr(child, "_subagent_id", None) or f"idx{task_index}"- ts = _dt.datetime.now().strftime("%Y%m%d_%H%M%S")- dump_path = logs_dir / f"subagent-timeout-{subagent_id}-{ts}.log"-- lines: List[str] = []- def _w(line: str = "") -> None:- lines.append(line)-- _w("# Subagent timeout diagnostic — issue #14726")- _w(f"# Generated: {_dt.datetime.now().isoformat()}")- _w("")- _w("## Timeout")- _w(f" task_index: {task_index}")- _w(f" subagent_id: {subagent_id}")- _w(f" configured_timeout: {timeout_seconds}s")- _w(f" actual_duration: {duration_seconds:.2f}s")- _w("")-- _w("## Goal")- _goal_preview = (goal or "").strip()- if len(_goal_preview) > 1000:- _goal_preview = _goal_preview[:1000] + " ...[truncated]"- _w(_goal_preview or "(empty)")- _w("")-- _w("## Child config")- for attr in (- "model", "provider", "api_mode", "base_url", "max_iterations",- "quiet_mode", "skip_memory", "skip_context_files", "platform",- "_delegate_role", "_delegate_depth",- ):- try:- val = getattr(child, attr, None)- # Redact api_key-shaped values defensively- if isinstance(val, str) and attr == "base_url":- pass- _w(f" {attr}: {val!r}")- except Exception:- _w(f" {attr}: <unreadable>")- _w("")-- _w("## Toolsets")- enabled = getattr(child, "enabled_toolsets", None)- _w(f" enabled_toolsets: {enabled!r}")- tool_names = getattr(child, "valid_tool_names", None)- if tool_names:- _w(f" loaded tool count: {len(tool_names)}")- try:- _w(f" loaded tools: {sorted(tool_names)}")- except Exception:- pass- _w("")-- _w("## Prompt / schema sizes")- try:- sys_prompt = getattr(child, "ephemeral_system_prompt", None) \- or getattr(child, "system_prompt", None) \- or ""- _w(f" system_prompt_bytes: {len(sys_prompt.encode('utf-8')) if isinstance(sys_prompt, str) else 'n/a'}")- _w(f" system_prompt_chars: {len(sys_prompt) if isinstance(sys_prompt, str) else 'n/a'}")- except Exception as exc:- _w(f" system_prompt: <error: {exc}>")- try:- tools_schema = getattr(child, "tools", None)- if tools_schema is not None:- _schema_json = json.dumps(tools_schema, default=str)- _w(f" tool_schema_count: {len(tools_schema)}")- _w(f" tool_schema_bytes: {len(_schema_json.encode('utf-8'))}")- except Exception as exc:- _w(f" tool_schema: <error: {exc}>")- _w("")-- _w("## Activity summary")- try:- summary = child.get_activity_summary()- for k, v in summary.items():- _w(f" {k}: {v!r}")- except Exception as exc:- _w(f" <get_activity_summary failed: {exc}>")- _w("")-- _w("## Worker thread stack at timeout")- if worker_thread is not None and worker_thread.is_alive():- frames = _sys._current_frames()- worker_frame = frames.get(worker_thread.ident)- if worker_frame is not None:- stack = _traceback.format_stack(worker_frame)- for frame_line in stack:- for sub in frame_line.rstrip().split("\n"):- _w(f" {sub}")- else:- _w(" <worker frame not available>")- elif worker_thread is None:- _w(" <no worker thread handle>")- else:- _w(" <worker thread already exited>")- _w("")-- # All other live threads. The conversation worker's own stack often- # shows it parked waiting on a nested helper thread (interrupt worker,- # daemon-pool sibling) — without the full picture, a pre-HTTP wedge- # (#60203/#62151) is indistinguishable from a slow provider. Best- # effort and bounded: names + stacks for up to 40 threads.- _w("## All thread stacks at timeout")- try:- frames = _sys._current_frames()- by_ident = {- th.ident: th for th in _threading.enumerate() if th.ident- }- worker_ident = worker_thread.ident if worker_thread else None- dumped = 0- for ident, frame in frames.items():- if ident == worker_ident:- continue # already dumped above- if dumped >= 40:- _w(f" <{len(frames) - dumped - 1} more threads omitted>")- break- th = by_ident.get(ident)- name = th.name if th else f"ident={ident}"- daemon = " daemon" if (th and th.daemon) else ""- _w(f" --- {name}{daemon} ---")- for frame_line in _traceback.format_stack(frame):- for sub in frame_line.rstrip().split("\n"):- _w(f" {sub}")- dumped += 1- except Exception as exc:- _w(f" <all-thread dump failed: {exc}>")- _w("")-- _w("## Notes")- _w(" This file is written ONLY when a subagent times out with 0 API calls.")- _w(" 0-API-call timeouts mean the child never reached its first LLM request.")- _w(" Common causes: oversized prompt rejected by provider, transport hang,")- _w(" credential resolution stuck. See issue #14726 for context.")-- dump_path.write_text("\n".join(lines), encoding="utf-8")- return str(dump_path)- except Exception as exc:- logger.warning("Subagent timeout diagnostic dump failed: %s", exc)- return None---def _spill_summary_to_file(task_index: int, summary: str) -> Optional[str]:- """Write a subagent's full summary to the delegation cache and return path.-- Mirrors web_extract's ``_store_full_text``: the file lands in- ``cache/delegation`` which is mounted read-only into remote backends- (Docker/Modal/SSH) via ``credential_files._CACHE_DIRS``, so the parent's- terminal/``read_file`` tools can page through the complete text on any- backend. Returns the absolute path, or None on failure (best-effort:- the trimmed head+tail is still returned to the parent regardless).- """- try:- from hermes_constants import get_hermes_dir- import datetime as _dt-- cache_dir = get_hermes_dir("cache/delegation", "delegation_cache")- cache_dir.mkdir(parents=True, exist_ok=True)- ts = _dt.datetime.now().strftime("%Y%m%d_%H%M%S_%f")- path = cache_dir / f"subagent-summary-{task_index}-{ts}.txt"- from tools.spill_safety import write_text_exclusive-- # Exclusive symlink-refusing create; not private because- # cache/delegation is bind-mounted read-only into remote backends- # whose container UID must be able to read it.- write_text_exclusive(path, summary, private=False)- return str(path)- except Exception as exc:- logger.debug("Failed to spill subagent summary to file: %s", exc)- return None---def _trim_summary_with_footer(- summary: str, cap: int, task_index: int-) -> tuple[str, Optional[str]]:- """Return (model_text, spill_path) for one over-budget summary.-- Mirrors web_extract's ``_truncate_with_footer``: keep a head+tail window- (~75% head / ~25% tail, snapped to line boundaries) so the subagent's- opening AND its closing (outcomes / files-changed / issues, which live at- the end) both survive, spill the full text to disk, and append a footer- telling the parent exactly how much it's seeing and the precise- ``read_file offset=`` to page into the omitted middle. Deterministic.- """- original_len = len(summary)- head_budget = int(cap * 0.75)- tail_budget = cap - head_budget-- head = summary[:head_budget]- tail = summary[-tail_budget:]- # Snap the head cut back to the last newline so we don't slice mid-line.- nl = head.rfind("\n")- if nl > head_budget * 0.5:- head = head[:nl]- # Snap the tail cut forward to the next newline for the same reason.- nl = tail.find("\n")- if 0 <= nl < tail_budget * 0.5:- tail = tail[nl + 1:]-- spill_path = _spill_summary_to_file(task_index, summary)-- footer_lines = [- "",- "─" * 8 + " [SUMMARY TRUNCATED] " + "─" * 8,- f"Showing {len(head):,} chars (head) + {len(tail):,} chars (tail) "- f"of {original_len:,} total — trimmed to protect the parent's context window.",- ]- if spill_path:- # read_file is 1-indexed; +2 moves past the last head line shown.- middle_start_line = head.count("\n") + 2- footer_lines.append(f"Full subagent output saved to: {spill_path}")- footer_lines.append(- f'To read the omitted middle: read_file path="{spill_path}" '- f"offset={middle_start_line} limit=200 (the file is the complete "- f"summary; raise/lower offset to page through it)."- )- else:- footer_lines.append(- "Full output could not be stored to disk; the head+tail above is "- "all that was preserved."- )- footer_lines.append("─" * 37)-- model_text = head + "\n\n[... middle omitted — see footer ...]\n\n" + tail + "\n".join(footer_lines)- return model_text, spill_path---def _parent_summary_char_budget(parent_agent, n_summaries: int) -> Optional[int]:- """Per-summary character budget sized against the parent's *remaining*- context headroom, split across the batch.-- The overflow this guards against is N summaries entering the parent- context at once (batch fan-out), not any single summary being large. We- take a fraction of the headroom the parent has left (resolved context- length minus what's already in its prompt) and divide it across the batch,- converting tokens→chars at the standard ~4 chars/token estimate.-- Returns the per-summary char budget, or None when the parent's context- state is unknown (no compressor / no token count) — in which case the- caller falls back to the static char ceiling only.- """- try:- compressor = getattr(parent_agent, "context_compressor", None)- context_length = getattr(compressor, "context_length", None)- if not isinstance(context_length, int) or context_length <= 0:- return None-- used_tokens = getattr(parent_agent, "session_prompt_tokens", 0)- if not isinstance(used_tokens, (int, float)) or used_tokens < 0:- used_tokens = 0-- # Reserve the compressor's output budget so we measure INPUT headroom.- reserved = getattr(compressor, "max_tokens", 0) or 0- headroom_tokens = context_length - int(used_tokens) - int(reserved)- if headroom_tokens <= 0:- # Parent is already over budget — give each summary only the floor.- return _MIN_SUMMARY_CHARS-- batch_token_budget = int(headroom_tokens * _SUMMARY_HEADROOM_FRACTION)- per_summary_tokens = batch_token_budget // max(1, n_summaries)- per_summary_chars = per_summary_tokens * 4 # ~4 chars/token- return max(_MIN_SUMMARY_CHARS, per_summary_chars)- except Exception:- logger.debug("Summary budget computation failed", exc_info=True)- return None---def _apply_summary_budget(results: List[Dict[str, Any]], parent_agent) -> None:- """Trim subagent summaries in-place so the batch can't overflow the- parent's context window, spilling full text to disk so nothing is lost.-- The effective per-summary cap is the MIN of:- - the dynamic headroom budget (remaining parent context ÷ batch size), and- - the static ``delegation.max_summary_chars`` ceiling (0 = disabled).-- When a summary exceeds the cap, its full text is written to a file and the- in-context summary becomes a head slice plus a pointer to that file. This- addresses issue/PR #9126: batch fan-out returned N full summaries verbatim,- blowing the parent context and (on rate-limited providers) triggering a- compression/429 death spiral.- """- summaries = [- r for r in results if isinstance(r, dict) and isinstance(r.get("summary"), str) and r["summary"]- ]- if not summaries:- return-- cfg = _load_config()- try:- static_ceiling = int(cfg.get("max_summary_chars", DEFAULT_MAX_SUMMARY_CHARS))- except (TypeError, ValueError):- static_ceiling = DEFAULT_MAX_SUMMARY_CHARS-- dynamic_budget = _parent_summary_char_budget(parent_agent, len(summaries))-- # Combine the two caps. Either can be absent/disabled.- candidates = [c for c in (static_ceiling, dynamic_budget) if c and c > 0]- if not candidates:- return # both disabled / unknown → leave summaries untouched- cap = min(candidates)-- for entry in summaries:- summary = entry["summary"]- if len(summary) <= cap:- continue- original_len = len(summary)- model_text, spill_path = _trim_summary_with_footer(- summary, cap, entry.get("task_index", -1)- )- entry["summary"] = model_text- entry["summary_truncated"] = True- if spill_path:- entry["summary_full_path"] = spill_path- logger.debug(- "[subagent-%s] summary trimmed %d → ~%d chars (spill=%s)",- entry.get("task_index", "?"),- original_len,- cap,- spill_path or "none",- )--def _run_single_child(task_index: int,goal: str,@@ -2624,264 +749,51 @@ def _run_single_child(owner_session_record: Any = None,**_kwargs,) -> Dict[str, Any]:- """- Run a pre-built child agent. Called from within a thread.- Returns a structured result dict with a ``status`` and ``exit_reason``- that are derived honestly from the child's structured completion fields.-- ``status`` ∈ {``"completed"``, ``"interrupted"``, ``"failed"``}:- * ``"completed"`` — the child reached a normal finish (may still have- hit its iteration budget; see ``exit_reason``).- * ``"interrupted"`` — the child was interrupted (``interrupted=True``).- * ``"failed"`` — a structured failure (``failed=True`` or a non-empty- ``error``) or a summary-less/invalid terminal state.-- ``exit_reason`` ∈ {``"completed"``, ``"max_iterations"``, ``"interrupted"``,- ``"error"``}:- * ``"completed"`` — normal finish.- * ``"max_iterations"`` — genuine per-child iteration-budget exhaustion- (``completed=False`` with no failure fields).- * ``"interrupted"`` — interrupted by the parent.- * ``"error"`` — provider rejection / terminal failure; NOT- budget exhaustion (this is the case #97655 fixed).-- ``truncated`` is derived as ``exit_reason == "max_iterations"`` only, so the- parent-visible truncation flag stays truthful for all of the above.+ """Run a pre-built child agent (called from a worker thread) and return its result entry.++ Contract, derived from the child's structured completion fields:+ status ∈ {completed, interrupted, failed} — a structured failure+ (failed=True / non-empty error) or an invalid terminal state+ is "failed" even when a summary exists.+ exit_reason ∈ {completed, max_iterations, interrupted, error} —+ "max_iterations" only for genuine budget exhaustion+ (completed=False with no failure fields), never for errors.+ truncated == (exit_reason == "max_iterations")."""child_start = time.monotonic()- # A timed-out Future may still be unwinding on its daemon worker. Closing- # the child from this owner thread before that Future settles races every- # resource the conversation's finally path still touches (notably its- # owned SessionDB). The timeout branch flips this when close ownership is- # handed to a Future done-callback instead.+ # Set when a timed-out Future still owns the child: closing it from this+ # thread before the worker settles races the conversation's finally path._child_close_deferred = False-- # Get the progress callback from the child agentchild_progress_cb = getattr(child, "tool_progress_callback", None)- # Restore parent tool names using the value saved before child construction- # mutated the global. This is the correct parent toolset, not the child's.- import model_tools-- _saved_tool_names = getattr(- child, "_delegate_saved_tool_names", list(model_tools._last_resolved_tool_names)- )-child_pool = getattr(child, "_credential_pool", None)leased_cred_id = Noneif child_pool is not None:leased_cred_id = child_pool.acquire_lease()- if leased_cred_id is not None:- try:- leased_entry = child_pool.current()- if leased_entry is not None and hasattr(child, "_swap_credential"):- child._swap_credential(leased_entry)- except Exception as exc:- logger.debug("Failed to bind child to leased credential: %s", exc)-- # Heartbeat: periodically propagate child activity to the parent so the- # gateway inactivity timeout doesn't fire while the subagent is working.- # Without this, the parent's _last_activity_ts freezes when delegate_task- # starts and the gateway eventually kills the agent for "no activity".- _heartbeat_stop = threading.Event()- # Stale detection: track the child's (tool, iteration, activity_ts) across- # heartbeat cycles. If none advances, count the cycle as stale.- # Different thresholds for idle vs in-tool (see _HEARTBEAT_STALE_CYCLES_*).- # last_activity_ts is the same liveness signal the async stall monitor- # already uses (streamed chunks + direct_api_call mid-wait heartbeats).- _last_seen_iter = [0]- _last_seen_tool = [None] # type: list- _last_seen_activity_ts = [None] # type: list- _stale_count = [0]-- def _heartbeat_loop():- while not _heartbeat_stop.wait(_HEARTBEAT_INTERVAL):- if parent_agent is None:- continue- touch = getattr(parent_agent, "_touch_activity", None)- if not touch:- continue- # Pull detail from the child's own activity tracker- desc = f"delegate_task: subagent {task_index} working"- try:- child_summary = child.get_activity_summary()- child_tool = child_summary.get("current_tool")- child_iter = child_summary.get("api_call_count", 0)- child_max = child_summary.get("max_iterations", 0)- child_activity_ts = child_summary.get("last_activity_ts")-- # Stale detection: count cycles where iteration, current_tool,- # AND last_activity_ts are all frozen. A child running a- # legitimately long-running tool keeps current_tool set; a- # child waiting on a slow model refreshes last_activity_ts- # via direct_api_call's activity heartbeat — neither should- # look stale at the idle threshold.- iter_advanced = child_iter > _last_seen_iter[0]- tool_changed = child_tool != _last_seen_tool[0]- activity_advanced = (- child_activity_ts is not None- and (- _last_seen_activity_ts[0] is None- or child_activity_ts > _last_seen_activity_ts[0]- )- )- if iter_advanced or tool_changed or activity_advanced:- _last_seen_iter[0] = child_iter- _last_seen_tool[0] = child_tool- if child_activity_ts is not None:- _last_seen_activity_ts[0] = child_activity_ts- _stale_count[0] = 0- else:- _stale_count[0] += 1-- # Pick threshold based on whether the child is currently- # inside a tool call. In-tool threshold is high enough to- # cover legitimately slow tools; idle threshold stays- # tight so the gateway timeout can fire on a truly wedged- # child.- stale_limit = (- _HEARTBEAT_STALE_CYCLES_IN_TOOL- if child_tool- else _HEARTBEAT_STALE_CYCLES_IDLE- )- if _stale_count[0] >= stale_limit:- logger.warning(- "Subagent %d appears stale (no progress for %d "- "heartbeat cycles, tool=%s) — stopping heartbeat",- task_index,- _stale_count[0],- child_tool or "<none>",- )- break # stop touching parent, let gateway timeout fire-- if child_tool:- desc = (- f"delegate_task: subagent running {child_tool} "- f"(iteration {child_iter}/{child_max})"- )- else:- child_desc = child_summary.get("last_activity_desc", "")- if child_desc:- desc = (- f"delegate_task: subagent {child_desc} "- f"(iteration {child_iter}/{child_max})"- )- except Exception:- pass- try:- touch(desc)- except Exception:- pass-- _heartbeat_thread = threading.Thread(target=_heartbeat_loop, daemon=True)-- # Register the live agent in the module-level registry so the TUI can- # target it by subagent_id (kill, pause, status queries). Unregistered- # in the finally block, even when the child raises. Test doubles that- # hand us a MagicMock don't carry stable ids; skip registration then.- _raw_sid = getattr(child, "_subagent_id", None)- _subagent_id = _raw_sid if isinstance(_raw_sid, str) else None- if _subagent_id:- if owner_session_id is None:- try:- from gateway.session_context import get_session_env-- owner_session_id = get_session_env("HERMES_UI_SESSION_ID", "") or None- except Exception:- owner_session_id = None- if owner_session_id and (- owner_transport is None or owner_session_record is None- ):- owner_transport, owner_session_record = (- _capture_gateway_steer_authority(owner_session_id)- )- _raw_depth = getattr(child, "_delegate_depth", 1)- _tui_depth = max(0, _raw_depth - 1) if isinstance(_raw_depth, int) else 0- _parent_sid = getattr(child, "_parent_subagent_id", None)- # Durable ownership spine: the OWNING CONVERSATION's session id (the- # same lineage the delivery path routes completions by). Sourced from- # the child's _parent_session_id stamp so it stays correct even when- # parent_agent has been rebuilt between dispatch and this run.- _owner_agent_session_id = (- str(getattr(child, "_parent_session_id", "") or "")- or str(getattr(parent_agent, "session_id", "") or "")- )- _delegation_id = getattr(child, "_delegation_id", None)- _register_subagent(- {- "subagent_id": _subagent_id,- "parent_id": _parent_sid if isinstance(_parent_sid, str) else None,- "depth": _tui_depth,- "goal": goal,- "delegation_id": (- _delegation_id if isinstance(_delegation_id, str) else None- ),- "model": (- getattr(child, "model", None)- if isinstance(getattr(child, "model", None), str)- else None- ),- "started_at": time.time(),- "status": "running",- "tool_count": 0,- "agent": child,- # Durable conversation lineage for the model-facing control- # plane (list/steer/stop). The weakref identity chain breaks- # when the CLI rebuilds its AIAgent mid-session; this id is- # the same spine completion delivery routes by.- "owner_agent_session_id": _owner_agent_session_id or None,- # Immutable live gateway/TUI session that commissioned this- # child. Empty outside those hosts; RPC authority fails closed.- "owner_session_id": owner_session_id,- "owner_transport": owner_transport,- "owner_session_record": owner_session_record,- }- )-- # Worktree-isolation state: populated inside the try once the child's- # task id is known; the default no-op keeps every early error path safe.- _worktree_info: Optional[Dict[str, str]] = None-- def _attach_worktree(entry_dict: Dict[str, Any]) -> None:- """Inspect + prune the child worktree, reporting into the entry."""- if _worktree_info is None:- return- try:- from tools import subagent_worktree-- entry_dict["worktree"] = (- subagent_worktree.finalize_subagent_worktree(_worktree_info)- )- except Exception as e:- # finalize is written hard not to raise, but if it ever does the- # state is unknown — emit the SAME schema the parent expects,- # flagged, via the shared factory so the two producers of this- # payload can never drift.- logger.warning("worktree finalize failed: %s", e)+ if leased_cred_id is not None:try:- from tools import subagent_worktree as _sw+ leased_entry = child_pool.current()+ if leased_entry is not None and hasattr(child, "_swap_credential"):+ child._swap_credential(leased_entry)+ except Exception as exc:+ logger.debug("Failed to bind child to leased credential: %s", exc)- entry_dict["worktree"] = _sw.unproven_worktree_payload(- _worktree_info, f"finalize raised: {e}"- )- except Exception:- # Import itself failed — inline the same shape rather than- # dropping the flag (the parent must still see the warning).- entry_dict["worktree"] = {- "path": _worktree_info.get("path", ""),- "branch": _worktree_info.get("branch", ""),- "commits": 0,- "dirty": False,- "pruned": False,- "inspection_failed": True,- "note": (- f"worktree finalize raised ({e}) and the reporting "- "helper was unavailable: 'commits' and 'dirty' are "- "UNKNOWN, not zero/clean. Inspect "- f"{_worktree_info.get('path', '')} before assuming "- "no work."- ),- }+ # Heartbeat keeps the parent's _last_activity_ts moving so the gateway+ # inactivity timeout doesn't fire while the child works; it stops itself+ # once the child looks stale (see _HEARTBEAT_STALE_CYCLES_*).+ heartbeat = _start_heartbeat(child, parent_agent, task_index)+ _heartbeat_thread = heartbeat[1]+ # TUI/RPC registry entry (kill/pause/status by subagent_id); None for test+ # doubles without a stable id. Unregistered in the finally block.+ _subagent_id = _register_child(+ child,+ parent_agent,+ goal,+ owner_session_id=owner_session_id,+ owner_transport=owner_transport,+ owner_session_record=owner_session_record,+ )+ worktree = _WorktreeReporter()try:_heartbeat_thread.start()@@ -2891,101 +803,24 @@ def _run_single_child(except Exception as e:logger.debug("Progress callback start failed: %s", e)- # File-state coordination: reuse the stable subagent_id as the child's- # task_id so file_state writes, active-subagents registry, and TUI- # events all share one key. Falls back to a fresh uuid only if the- # pre-built id is somehow missing.- import uuid as _uuid-- child_task_id = _subagent_id or f"subagent-{task_index}-{_uuid.uuid4().hex[:8]}"- parent_task_id = getattr(parent_agent, "_current_task_id", None)- # Seed the child's session-cwd record from the parent's (cwd rearch):- # children share the parent's container, and today they inherit the- # parent's live env.cwd implicitly. Seeding at spawn preserves that- # starting directory while keeping the child's subsequent `cd`s- # isolated in its own record (a child's cd no longer bleeds back into- # the parent once readers flip to the record store).- try:- from tools.terminal_tool import (- get_session_cwd,- record_session_cwd,- register_container_alias,- )-- record_session_cwd(child_task_id, get_session_cwd(parent_task_id))- # Per-session container isolation (docker + container_persistent:- # false) keys containers by session task_id. The child must share- # the PARENT's container — register the alias so the child's- # task_id resolves to the parent's container key.- register_container_alias(child_task_id, parent_task_id)- except Exception as e:- logger.debug("Child cwd seed failed: %s", e)-- # Opt-in worktree isolation (delegation.worktree_isolation, inspired- # by Muse Code's --subagent-worktree-isolation): give this child its- # own git worktree branched from the parent repo's HEAD, and start its- # terminal there. Git-only and local-backend-only; any failure- # degrades silently to the shared-workspace behavior above.- if _get_worktree_isolation():- try:- from tools import subagent_worktree-- if subagent_worktree.local_backend_active():- _parent_cwd = None- try:- from tools.terminal_tool import get_session_cwd as _gsc-- _parent_cwd = _gsc(parent_task_id)- except Exception:- pass- _worktree_info = subagent_worktree.create_subagent_worktree(- _parent_cwd or _resolve_workspace_hint(parent_agent),- subagent_id=_subagent_id,- )- else:- logger.debug(- "worktree isolation skipped: non-local terminal backend"- )- except Exception as e:- logger.debug("worktree isolation setup failed: %s", e)- if _worktree_info is not None:- try:- from tools.terminal_tool import record_session_cwd as _rsc-- _rsc(child_task_id, _worktree_info["path"])- except Exception as e:- logger.debug("worktree cwd seed failed: %s", e)- # The child's context is already built; carry the isolation- # contract on the goal message instead (same turn, no- # system-prompt mutation).- from tools.subagent_worktree import build_worktree_context_note+ ws = _seed_child_workspace(child, parent_agent, goal, task_index, _subagent_id, worktree)+ goal = ws.goal- goal = goal + build_worktree_context_note(_worktree_info)-- wall_start = time.time()- parent_reads_snapshot = (- list(file_state.known_reads(parent_task_id)) if parent_task_id else []- )-- # Run child with an optional hard timeout (off by default —- # result(timeout=None) blocks until the child finishes). Stuck-child- # protection comes from the heartbeat staleness monitor instead.+ # Optional hard timeout (off by default — result(timeout=None) blocks+ # until the child finishes; stuck-child protection is the heartbeat).child_timeout = _get_child_timeout()- # Daemon worker (tools.daemon_pool): a timed-out child is abandoned- # below; a stdlib non-daemon worker would then block interpreter- # exit at atexit-join time if the child never unwinds.+ # Daemon worker: a timed-out child is abandoned below and a stdlib+ # non-daemon worker would block interpreter exit at atexit-join time.from tools.daemon_pool import DaemonThreadPoolExecutor_timeout_executor = DaemonThreadPoolExecutor(max_workers=1,- # Install a non-interactive approval callback in the worker thread- # so dangerous-command prompts from the subagent don't fall back to- # input() and deadlock the parent's prompt_toolkit TUI.- # Callback (deny vs approve) is governed by delegation.subagent_auto_approve.+ # Non-interactive approval callback in the worker thread so dangerous+ # command prompts never fall back to input() and deadlock the parent+ # TUI; deny vs approve follows delegation.subagent_auto_approve.initializer=_set_subagent_approval_cb,initargs=(_get_subagent_approval_callback(),),)- # Capture the worker thread so the timeout diagnostic can dump its- # Python stack (see #14726 — 0-API-call hangs are opaque without it).+ # Worker thread handle so the timeout diagnostic can dump its stack._worker_thread_holder: Dict[str, Optional[threading.Thread]] = {"t": None}def _relay_child_text(delta: str) -> None:@@ -3006,7 +841,7 @@ def _run_single_child(with delegated_child_context(str(getattr(child, "session_id", "") or "")):return child.run_conversation(user_message=goal,- task_id=child_task_id,+ task_id=ws.child_task_id,stream_callback=_relay_child_text,)@@ -3018,248 +853,34 @@ def _run_single_child(try:result = _child_future.result(timeout=child_timeout)except Exception as _timeout_exc:- # No consumer boundary remains once this owner stops waiting for- # the child. Close acceptance before any completion callback and- # retain steer text that won the race with this failure/timeout.- _late_pending_steer = (- _close_subagent_steering(_subagent_id, child) if _subagent_id else None- )- # Signal the child to stop so its thread can exit cleanly.- try:- interrupted = child is not None and request_hard_interrupt(child)- if not interrupted and child is not None and hasattr(child, "_interrupt_requested"):- child._interrupt_requested = True- except Exception:- pass-- is_timeout = isinstance(_timeout_exc, (FuturesTimeoutError, TimeoutError))- duration = round(time.monotonic() - child_start, 2)- logger.warning(- "Subagent %d %s after %.1fs",- task_index,- "timed out" if is_timeout else f"raised {type(_timeout_exc).__name__}",- duration,+ failure = _handle_child_wait_failure(+ _timeout_exc,+ child=child,+ task_index=task_index,+ goal=goal,+ subagent_id=_subagent_id,+ child_future=_child_future,+ child_timeout=child_timeout,+ child_start=child_start,+ child_progress_cb=child_progress_cb,+ worker_thread_holder=_worker_thread_holder,+ worktree=worktree,)-- # When a subagent times out BEFORE making any API call, dump a- # diagnostic to help users (and us) see what the child was doing.- # See #14726 — without this, 0-API-call hangs are black boxes.- diagnostic_path: Optional[str] = None- child_api_calls = 0- try:- _summary = child.get_activity_summary()- child_api_calls = int(_summary.get("api_call_count", 0) or 0)- except Exception:- pass- if is_timeout and child_api_calls == 0:- diagnostic_path = _dump_subagent_timeout_diagnostic(- child=child,- task_index=task_index,- # is_timeout implies a cap was configured (result(timeout=None)- # never raises FuturesTimeoutError); guard for the type checker.- timeout_seconds=float(child_timeout or 0.0),- duration_seconds=float(duration),- worker_thread=_worker_thread_holder.get("t"),- goal=goal,- )- if diagnostic_path:- logger.warning(- "Subagent %d 0-API-call timeout — diagnostic written to %s",- task_index,- diagnostic_path,- )-- if child_progress_cb:- try:- child_progress_cb(- "subagent.complete",- preview=(- f"Timed out after {duration}s"- if is_timeout- else str(_timeout_exc)- ),- status="timeout" if is_timeout else "error",- duration_seconds=duration,- summary="",- )- except Exception:- pass-- if is_timeout:- if child_api_calls == 0:- _err = (- f"Subagent timed out after {child_timeout}s without "- f"making any API call — the child never reached its "- f"first LLM request (prompt construction, credential "- f"resolution, or transport may be stuck)."- )- if diagnostic_path:- _err += f" Diagnostic: {diagnostic_path}"- else:- _err = (- f"Subagent timed out after {child_timeout}s with "- f"{child_api_calls} API call(s) completed — likely "- f"stuck on a slow API call, tool call, or unresponsive "- f"network request."- )- if diagnostic_path:- _err += f" Diagnostic: {diagnostic_path}"- else:- _err = str(_timeout_exc)-- _error_entry = {- "task_index": task_index,- "status": "timeout" if is_timeout else "error",- "summary": None,- "error": _err,- "exit_reason": "timeout" if is_timeout else "error",- "api_calls": child_api_calls,- "duration_seconds": duration,- "timeout_seconds": child_timeout if is_timeout else None,- "timed_out_after_seconds": duration if is_timeout else None,- "timeout_phase": (- "before_first_llm_call" if is_timeout and child_api_calls == 0- else "after_llm_calls" if is_timeout- else None- ),- "_child_role": getattr(child, "_delegate_role", None),- "diagnostic_path": diagnostic_path,- }- if _late_pending_steer:- _error_entry["missed_steer"] = _late_pending_steer- _error_entry["error"] += (- " [steer did not land before the subagent stopped: "- f"{_late_pending_steer}]"- )- _attach_worktree(_error_entry)- if is_timeout and not _child_future.done():- # request_hard_interrupt() is cooperative: the worker still- # executes run_conversation's finally path before its Future- # becomes done. child.close() tears down that same agent's- # clients, messages, and owned SQLite handle, so calling it in- # our outer finally while the worker is alive can close SQLite- # underneath its final activity write. Future callbacks run- # only after the worker has fully returned (or raised), which- # is the first safe close boundary.- def _close_after_timed_out_worker(_done_future) -> None:- try:- close = getattr(child, "close", None)- if callable(close):- close()- except Exception:- logger.debug(- "Failed to close timed-out child after worker exit",- exc_info=True,- )-- _child_future.add_done_callback(_close_after_timed_out_worker)- _child_close_deferred = True-- # Bounded drain (#94248 native half): the deferred close above- # only fires once the abandoned worker unwinds, but that worker- # is typically parked inside an in-flight OpenSSL read (Codex /- # httpx). Never hard-close that transport from this thread —- # releasing FDs under a live SSL read is the #29507/#70773- # native-corruption family. Instead shutdown() the child's- # pooled sockets, which is FD-safe from any thread and settles- # the blocked read with EOF/EPIPE so the worker can unwind and- # trigger the deferred close. One immediate sweep plus one- # delayed re-sweep (covers a fresh connection opened between- # the interrupt and the first sweep); a worker that still- # doesn't settle keeps its resources until process exit rather- # than risking a cross-thread FD release.- _drain = getattr(child, "_drain_transports_after_abandonment", None)- if callable(_drain):- def _drain_once(phase: str) -> None:- try:- _drain(reason=f"delegate_timeout_{phase}")- except Exception:- logger.debug(- "Timed-out child transport drain (%s) failed",- phase,- exc_info=True,- )-- _drain_once("immediate")-- def _drain_resweep() -> None:- if not _child_future.done():- _drain_once("resweep")-- _resweep_timer = threading.Timer(5.0, _drain_resweep)- _resweep_timer.daemon = True- _resweep_timer.start()- return _error_entry+ _child_close_deferred = failure.close_deferred+ return failure.entryfinally:- # Shut down executor without waiting — if the child thread- # is stuck on blocking I/O, wait=True would hang forever.+ # Shut down without waiting — a child stuck on blocking I/O would+ # hang wait=True forever._timeout_executor.shutdown(wait=False)- # T1-24: structured-output contract validation + ONE bounded retry.- # Runs only when a schema was attached at dispatch; schema-less- # delegations take none of these branches and their result entry- # stays byte-identical (wire-shape pinning).- # Pattern from: github/copilot-cli ctx.agent(prompt, {schema}) —- # PATTERN ONLY, no code copied.- _output_schema = getattr(child, "_delegate_output_schema", None)- _schema_valid: Optional[bool] = None- _schema_errors: List[str] = []- _schema_retries = 0- if isinstance(_output_schema, dict):- from tools.delegation_output_schema import (- build_retry_message,- validate_output,- )-- _first_text = result.get("final_response") or ""- _schema_valid, _schema_errors = validate_output(- _first_text, _output_schema- )- if (- not _schema_valid- and _first_text.strip()- and not result.get("interrupted", False)- ):- # Exactly one retry turn, carrying the validation errors- # verbatim (no schema re-paste — the child already holds- # the contract in its context).- _schema_retries = 1- _retry_result = None- try:- _retry_result = child.run_conversation(- user_message=build_retry_message(_schema_errors),- task_id=child_task_id,- stream_callback=_relay_child_text,- )- except Exception as _retry_exc:- logger.warning(- "Subagent %d schema-retry turn failed: %s",- task_index,- _retry_exc,- )- if isinstance(_retry_result, dict):- _retry_text = _retry_result.get("final_response") or ""- if _retry_text.strip():- result["final_response"] = _retry_text- try:- result["api_calls"] = int(- result.get("api_calls", 0) or 0- ) + int(_retry_result.get("api_calls", 0) or 0)- except (TypeError, ValueError):- pass- _retry_messages = _retry_result.get("messages")- if isinstance(_retry_messages, list) and isinstance(- result.get("messages"), list- ):- result["messages"] = result["messages"] + _retry_messages- _schema_valid, _schema_errors = validate_output(- _retry_text, _output_schema- )+ schema = _validate_child_output_schema(+ child, result, task_index, ws.child_task_id, _relay_child_text+ )- # Linearization boundary for registry steering. From this point on the- # child cannot consume another steer. Closing under the registry lock- # either rejects a concurrent caller or drains every previously accepted- # exact text into the result before callbacks/result assembly can run.+ # Linearization boundary for registry steering: from here the child+ # cannot consume another steer. Closing under the registry lock either+ # rejects a concurrent caller or drains every accepted exact text into+ # the result before callbacks/result assembly run._late_pending_steer = (_close_subagent_steering(_subagent_id, child) if _subagent_id else None)@@ -3279,306 +900,10 @@ def _run_single_child(logger.debug("Progress callback flush failed: %s", e)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)-- # The child emits the literal "(empty)" sentinel (see run_agent.py) when- # it gives up after repeated empty-LLM-response retries — typically a- # transport bug (misrouted provider, adapter returning empty- # ChatCompletion, etc.). Treat it as a failure so the parent surfaces- # it instead of silently accepting zero-content "success".- _empty_sentinel = summary.strip() == "(empty)"-- if interrupted:- status = "interrupted"- elif result.get("failed") or result.get("error"):- # A structured failure (provider rejection / terminal exception)- # must WIN over the summary-presence heuristic below. The child's- # conversation loop returns the error text as final_response, so an- # error-shaped summary would otherwise be labeled "completed" here- # despite completed=False. The heuristic is only a fallback for- # legacy/mock results that omit the structured failure fields.- # (Community report Aug 2026; #97655.)- status = "failed"- elif _schema_valid is False:- # T1-24 follow-up: a schema was declared and the final answer —- # after the one bounded retry — still violates it (empty `{}`- # fallback included). A summary exists, but it is unusable under- # the contract the caller asked for, so it must not be reported- # as a completed delegation: the batch line would print ✓ and- # orchestrators that read only status/icon would accept an- # empty verdict. schema_valid/schema_errors (below) carry the- # detail; status has to agree with them. _schema_valid stays- # None on schema-less runs, which never take this branch.- status = "failed"- elif summary and not _empty_sentinel:- # A summary means the subagent produced usable output.- # exit_reason ("completed" vs "max_iterations") already- # tells the parent *how* the task ended.- status = "completed"- else:- status = "failed"-- # Build tool trace from conversation messages (already in memory).- # Uses tool_call_id to correctly pair parallel tool calls with results.- tool_trace: list[Dict[str, Any]] = []- trace_by_id: Dict[str, Dict[str, Any]] = {}- messages = result.get("messages") or []- if isinstance(messages, list):- for msg in messages:- if not isinstance(msg, dict):- continue- if msg.get("role") == "assistant":- for tc in msg.get("tool_calls") or []:- fn = tc.get("function", {})- arguments = fn.get("arguments", "")- entry_t = {- "tool": fn.get("name", "unknown"),- "args_bytes": len(arguments),- "input_summary": _summarize_tool_arguments(arguments),- }- tool_trace.append(entry_t)- tc_id = tc.get("id")- if tc_id:- trace_by_id[tc_id] = entry_t- elif msg.get("role") == "tool":- content = _stringify_tool_content(msg.get("content", ""))- is_error = _looks_like_error_output(content)- result_meta = {- "result_bytes": len(content),- "status": "error" if is_error else "ok",- }- # Match by tool_call_id for parallel calls- tc_id = msg.get("tool_call_id")- target = trace_by_id.get(tc_id) if tc_id else None- if target is not None:- target.update(result_meta)- elif tool_trace:- # Fallback for messages without tool_call_id- tool_trace[-1].update(result_meta)-- # Determine exit reason- if interrupted:- exit_reason = "interrupted"- elif result.get("failed") or result.get("error"):- # Provider rejection / terminal failure. Do NOT report this as- # iteration-budget exhaustion — "max_iterations" is only truthful- # when the child actually hit its per-delegation iteration cap.- exit_reason = "error"- elif completed:- exit_reason = "completed"- else:- # Genuine budget exhaustion: completed=False with no failure.- exit_reason = "max_iterations"-- # Extract token counts (safe for mock objects)- _input_tokens = getattr(child, "session_prompt_tokens", 0)- _output_tokens = getattr(child, "session_completion_tokens", 0)- _model = getattr(child, "model", None)-- # --- result entry contract (see _run_single_child docstring) ---- # status ∈ {completed, interrupted, failed}- # exit_reason ∈ {completed, max_iterations, interrupted, error}- # truncated is exactly (exit_reason == "max_iterations").- entry: Dict[str, Any] = {- "task_index": task_index,- "status": status,- "summary": summary,- "api_calls": api_calls,- "duration_seconds": duration,- "model": _model if isinstance(_model, str) else None,- "exit_reason": exit_reason,- # Explicit, parent-visible truncation flag. A subagent that- # exhausts its per-child iteration budget still returns a summary,- # so `status` stays "completed" (see above) — without this the- # parent can't tell truncated-but-summarized from cleanly-finished- # work except by parsing the summary prose. exit_reason is computed- # authoritatively from the child's `completed` flag.- "truncated": exit_reason == "max_iterations",- "tokens": {- "input": (- _input_tokens if isinstance(_input_tokens, (int, float)) else 0- ),- "output": (- _output_tokens if isinstance(_output_tokens, (int, float)) else 0- ),- },- "tool_trace": tool_trace,- # Captured before the finally block calls child.close() so the- # parent thread can fire subagent_stop with the correct role.- # Stripped before the dict is serialised back to the model.- "_child_role": getattr(child, "_delegate_role", None),- # Captured before child.close() so the parent aggregator can fold- # the child's total spend into the parent's session cost. Port of- # Kilo-Org/kilocode#9448 — previously the footer only reflected the- # parent's direct API calls and under-counted subagent-heavy runs.- # Stripped before the dict is serialised back to the model.- "_child_cost_usd": (- float(getattr(child, "session_estimated_cost_usd", 0.0) or 0.0)- if isinstance(- getattr(child, "session_estimated_cost_usd", 0.0),- (int, float),- )- else 0.0- ),- }- # Per-delegation spend, serialized back to the model alongside- # tokens/api_calls so the parent can see what each delegation cost.- # Mirrors _child_cost_usd (which is stripped pre-serialization and- # only feeds the parent session rollup).- # Inspired by: Perplexity Agent API result shape (idea-level).- entry["cost_usd"] = round(entry["_child_cost_usd"], 6)- _cost_status = getattr(child, "session_cost_status", None)- entry["cost_status"] = (- _cost_status if isinstance(_cost_status, str) and _cost_status- else "unknown"- )- if status == "failed":- if _schema_valid is False and summary and not _empty_sentinel:- # The child DID respond — the response just violates the- # declared contract. Name that instead of the generic- # "no response" error; schema_errors (below) hold the- # validator's specifics verbatim.- entry["error"] = (- "Final answer does not satisfy the declared "- "output_schema (after 1 retry)."- if _schema_retries- else "Final answer does not satisfy the declared "- "output_schema."- )- else:- entry["error"] = result.get(- "error", "Subagent did not produce a response."- )- # Classified reason from the child loop (e.g. "rate_limit",- # "billing", "server_error") — lets the parent distinguish a- # quota wall from a real task error without parsing prose.- _failure_reason = result.get("failure_reason")- if isinstance(_failure_reason, str) and _failure_reason:- entry["failure_reason"] = _failure_reason-- # T1-24: schema-validation outcome — emitted ONLY when a schema was- # requested, so legacy (schema-less) payloads keep their exact shape.- if isinstance(_output_schema, dict):- entry["schema_valid"] = bool(_schema_valid)- if _schema_retries:- entry["schema_retries"] = _schema_retries- if not _schema_valid and _schema_errors:- entry["schema_errors"] = _schema_errors-- # A steer that queued after the child's final assistant turn had no- # tool batch left to drain into. The finalizer hands the undelivered- # text back (turn_finalizer.py "pending_steer"); retain it here so the- # parent sees the steer was MISSED rather than silently absorbed —- # steer_subagent() returning True means "queued", and this is where a- # queued-but-never-delivered steer gets named.- _missed_steer = result.get("pending_steer")- if isinstance(_missed_steer, str) and _missed_steer.strip():- entry["missed_steer"] = _missed_steer- _miss_note = (- "[steer did not land — the subagent finished before it could "- f"be delivered: {_missed_steer}]"- )- entry["summary"] = f"{summary}\n\n{_miss_note}" if summary else _miss_note-- # Cross-agent file-state reminder. If this subagent wrote any- # files the parent had already read, surface it so the parent- # knows to re-read before editing — the scenario that motivated- # the registry. We check writes by ANY non-parent task_id (not- # just this child's), which also covers transitive writes from- # nested orchestrator→worker chains.- try:- if parent_task_id and parent_reads_snapshot:- sibling_writes = file_state.writes_since(- parent_task_id, wall_start, parent_reads_snapshot- )- if sibling_writes:- mod_paths = sorted(- {p for paths in sibling_writes.values() for p in paths}- )- if mod_paths:- reminder = (- "\n\n[NOTE: subagent modified files the parent "- "previously read — re-read before editing: "- + ", ".join(mod_paths[:8])- + (- f" (+{len(mod_paths) - 8} more)"- if len(mod_paths) > 8- else ""- )- + "]"- )- if entry.get("summary"):- entry["summary"] = entry["summary"] + reminder- else:- entry["stale_paths"] = mod_paths- except Exception:- logger.debug("file_state sibling-write check failed", exc_info=True)-- # Per-branch observability payload: tokens, cost, files touched, and- # a tail of tool-call results. Fed into the TUI's overlay detail- # pane + accordion rollups (features 1, 2, 4). All fields are- # optional — missing data degrades gracefully on the client.- _cost_usd = getattr(child, "session_estimated_cost_usd", None)- _reasoning_tokens = getattr(child, "session_reasoning_tokens", 0)- try:- _files_read = list(file_state.known_reads(child_task_id))[:40]- except Exception:- _files_read = []- try:- _files_written_map = file_state.writes_since(- "", wall_start, []- ) # all writes since wall_start- except Exception:- _files_written_map = {}- _files_written = sorted(- {- p- for tid, paths in _files_written_map.items()- if tid == child_task_id- for p in paths- }- )[:40]-- _output_tail = _extract_output_tail(result, max_entries=8, max_chars=600)-- complete_kwargs: Dict[str, Any] = {- "preview": summary[:160] if summary else entry.get("error", ""),- "status": status,- "duration_seconds": duration,- "summary": summary[:500] if summary else entry.get("error", ""),- "input_tokens": (- int(_input_tokens) if isinstance(_input_tokens, (int, float)) else 0- ),- "output_tokens": (- int(_output_tokens) if isinstance(_output_tokens, (int, float)) else 0- ),- "reasoning_tokens": (- int(_reasoning_tokens)- if isinstance(_reasoning_tokens, (int, float))- else 0- ),- "api_calls": int(api_calls) if isinstance(api_calls, (int, float)) else 0,- "files_read": _files_read,- "files_written": _files_written,- "output_tail": _output_tail,- }- if _cost_usd is not None:- try:- complete_kwargs["cost_usd"] = float(_cost_usd)- except (TypeError, ValueError):- pass-- if child_progress_cb:- try:- child_progress_cb("subagent.complete", **complete_kwargs)- except Exception as e:- logger.debug("Progress callback completion failed: %s", e)-- _attach_worktree(entry)+ entry = _build_result_entry(child, result, task_index, duration, schema)+ _append_sibling_write_reminder(entry, ws)+ _emit_child_complete(child, result, entry, ws, duration, child_progress_cb)+ worktree.attach(entry)return entryexcept Exception as exc:@@ -3613,224 +938,19 @@ def _run_single_child(" [steer did not land before the subagent stopped: "f"{_late_pending_steer}]")- # _attach_worktree defaults to a no-op when isolation never engaged.- _attach_worktree(_error_entry)+ worktree.attach(_error_entry) # no-op when isolation never engagedreturn _error_entryfinally:- # Stop the heartbeat thread so it doesn't keep touching parent activity- # after the child has finished (or failed). Guard the join: .start()- # now lives inside the try block, so if it raised (OS thread- # exhaustion) the thread was never started and Thread.join() would- # raise RuntimeError. ident is None until start() succeeds.- _heartbeat_stop.set()- if _heartbeat_thread.ident is not None:- _heartbeat_thread.join(timeout=5)-- # Drop the TUI-facing registry entry. Safe to call even if the- # child was never registered (e.g. ID missing on test doubles).- if _subagent_id:- _unregister_subagent(_subagent_id, agent=child)-- if child_pool is not None and leased_cred_id is not None:- try:- child_pool.release_lease(leased_cred_id)- except Exception as exc:- logger.debug("Failed to release credential lease: %s", exc)-- # Restore the parent's tool names so the process-global is correct- # for any subsequent execute_code calls or other consumers.- import model_tools-- saved_tool_names = getattr(child, "_delegate_saved_tool_names", None)- if isinstance(saved_tool_names, list):- model_tools._last_resolved_tool_names = list(saved_tool_names)-- # Remove child from active tracking-- # Unregister child from interrupt propagation- if hasattr(parent_agent, "_active_children"):- try:- lock = getattr(parent_agent, "_active_children_lock", None)- if lock:- with lock:- parent_agent._active_children.remove(child)- else:- parent_agent._active_children.remove(child)- except (ValueError, UnboundLocalError) as e:- logger.debug("Could not remove child from active_children: %s", e)-- # Close tool resources (terminal sandboxes, browser daemons,- # background processes, httpx clients) so subagent subprocesses- # don't outlive the delegation.- if not _child_close_deferred:- try:- close = getattr(child, "close", None)- if callable(close):- close()- except Exception:- logger.debug("Failed to close child agent after delegation")-- # The AIAgent turn boundary normally closes the child scope itself. This- # fallback covers failures before that boundary starts, but must not pop- # a scope while a timed-out child worker is still unwinding.- try:- from agent import relay_runtime-- runtime = relay_runtime.get_runtime(create=False)- child_session_id = str(getattr(child, "session_id", "") or "")- child_turn_is_active = relay_runtime.SESSION_COORDINATOR.has_active_turn(- profile_key=relay_runtime.current_profile_key(),- session_id=child_session_id,- )- if runtime is not None and child_session_id and not child_turn_is_active:- runtime.unregister_subagent({"child_session_id": child_session_id})- except Exception:- logger.debug("Failed to close child Relay session after delegation")---_PARENT_FINALIZATION_LOCK_GUARD = threading.Lock()-_PARENT_FINALIZATION_FALLBACK_LOCK = threading.RLock()-_CHILD_CONSTRUCTION_LOCK = threading.RLock()---def _build_child_preserving_parent_tools(**kwargs):- """Build a child without leaking its resolved toolset into the parent."""- import model_tools-- with _CHILD_CONSTRUCTION_LOCK:- parent_tool_names = list(model_tools._last_resolved_tool_names)- try:- child = _build_child_agent(**kwargs)- finally:- model_tools._last_resolved_tool_names = parent_tool_names- child._delegate_saved_tool_names = parent_tool_names- return child---def _parent_finalization_lock(parent_agent) -> threading.RLock:- """Return the per-parent lock that serializes lifecycle side effects."""- if parent_agent is None:- return _PARENT_FINALIZATION_FALLBACK_LOCK- lock = getattr(parent_agent, "_subagent_finalization_lock", None)- if lock is not None:- return lock- with _PARENT_FINALIZATION_LOCK_GUARD:- lock = getattr(parent_agent, "_subagent_finalization_lock", None)- if lock is None:- lock = threading.RLock()- try:- setattr(parent_agent, "_subagent_finalization_lock", lock)- except Exception:- return _PARENT_FINALIZATION_FALLBACK_LOCK- return lock---def _finalize_child_results(- results: List[Dict[str, Any]],- task_list: List[Dict[str, Any]],- children: List[tuple[int, Dict[str, Any], Any]],- parent_agent,-) -> None:- """Apply host-owned summary, memory, hook, and cost contracts once."""- with _parent_finalization_lock(parent_agent):- _apply_summary_budget(results, parent_agent)- child_by_index = {index: child for index, _task, child in children}-- if parent_agent and getattr(parent_agent, "_memory_manager", None):- for entry in results:- try:- task_index = entry.get("task_index", -1)- task_goal = (- task_list[task_index]["goal"]- if isinstance(task_index, int)- and 0 <= task_index < len(task_list)- else ""- )- child = child_by_index.get(task_index)- parent_agent._memory_manager.on_delegation(- task=task_goal,- result=entry.get("summary", "") or "",- child_session_id=getattr(child, "session_id", ""),- )- except Exception:- pass-- parent_session_id = getattr(parent_agent, "session_id", None)- try:- from hermes_cli.plugins import invoke_hook as invoke_hook- except Exception:- invoke_hook = None-- children_cost_total = 0.0- for entry in results:- child_role = entry.pop("_child_role", None)- child_cost = entry.pop("_child_cost_usd", 0.0)- try:- if child_cost:- children_cost_total += float(child_cost)- except (TypeError, ValueError):- pass- if invoke_hook is None:- continue- try:- child_index = entry.get("task_index", -1)- child = child_by_index.get(child_index)- invoke_hook(- "subagent_stop",- parent_session_id=parent_session_id,- parent_turn_id=getattr(parent_agent, "_current_turn_id", "") or "",- child_session_id=getattr(child, "session_id", None),- child_role=child_role,- child_summary=entry.get("summary"),- child_status=entry.get("status"),- tool_call_history=_subagent_stop_tool_call_history(- entry.get("tool_trace")- ),- duration_ms=int((entry.get("duration_seconds") or 0) * 1000),- )- except Exception:- logger.debug("subagent_stop hook invocation failed", exc_info=True)-- if children_cost_total > 0.0:- try:- current = float(- getattr(parent_agent, "session_estimated_cost_usd", 0.0) or 0.0- )- parent_agent.session_estimated_cost_usd = current + children_cost_total- if getattr(parent_agent, "session_cost_source", "none") in {- None,- "",- "none",- }:- parent_agent.session_cost_source = "subagent"- if getattr(parent_agent, "session_cost_status", "unknown") in {- None,- "",- "unknown",- }:- parent_agent.session_cost_status = "estimated"- except Exception:- logger.debug("Subagent cost rollup failed", exc_info=True)---def _run_child_lifecycle(- task_index: int,- goal: str,- child=None,- parent_agent=None,-) -> Dict[str, Any]:- """Run one child and apply the same host lifecycle used by delegate_task."""- result = _run_single_child(task_index, goal, child, parent_agent)- result.setdefault("task_index", task_index)- task = {"goal": goal}- _finalize_child_results(- [result],- [{"goal": ""} for _ in range(task_index)] + [task],- [(task_index, task, child)],- parent_agent,- )- return result+ _cleanup_child_run(+ child,+ parent_agent,+ subagent_id=_subagent_id,+ heartbeat=heartbeat,+ child_pool=child_pool,+ leased_cred_id=leased_cred_id,+ close_deferred=_child_close_deferred,+ )def _recover_tasks_from_json_string(@@ -3935,33 +1055,16 @@ def delegate_task(parent_agent=None,credentials_cfg: Optional[Dict[str, Any]] = None,) -> str:- """- Spawn one or more child agents to handle delegated tasks, or control- already-running ones.-- Spawn modes (action='spawn' or omitted):- - Single: provide goal (+ optional context and role)- - Batch: provide tasks array [{goal, context, role}, ...]-- Control modes (synchronous, never backgrounded):- - action='list' -> live children of this conversation's spawn tree- - action='steer' -> queue course-correction text into a running child- (subagent_id + message)- - action='stop' -> interrupt a running child early (subagent_id)+ """Spawn child agents (single ``goal`` or ``tasks=[...]`` batch) or control running ones.- The 'role' parameter controls whether a child can further delegate:- 'leaf' (default) cannot; 'orchestrator' retains the delegation- toolset and can spawn its own workers, bounded by- delegation.max_spawn_depth. Per-task role beats the top-level one.-- Returns JSON with results array, one entry per task.+ ``action`` list/steer/stop run synchronously and bypass the pause gate,+ depth limit and async dispatch. ``role`` is legacy (per-task beats+ top-level; capability is depth-derived). Returns JSON with one results+ entry per task, or a dispatch handle when running in the background."""if parent_agent is None:return tool_error("delegate_task requires a parent agent context.")- # ── Control plane: list/steer/stop run synchronously and return here.- # They never spawn, so they bypass the pause gate, depth limit, and the- # async dispatch machinery entirely.normalized_action = (action or "").strip().lower()if normalized_action in _CONTROL_ACTIONS:return _handle_control_action(@@ -3972,29 +1075,18 @@ def delegate_task(f"Unknown action '{action}'. Use spawn (default), list, steer, or stop.")- # Operator-controlled kill switch — lets the TUI freeze new fan-out- # when a runaway tree is detected, without interrupting already-running- # children. Cleared via the matching `delegation.pause` RPC.+ # Operator kill switch (TUI / delegation.pause RPC): blocks NEW spawns only.if is_spawn_paused():return tool_error("Delegation spawning is paused. Clear the pause via the TUI ""(`p` in /agents) or the `delegation.pause` RPC before retrying.")- # Normalise the top-level role once; per-task overrides re-normalise.top_role = _normalize_role(role)-- # Background (async) delegation now applies to BOTH single tasks and- # batches. A batch is dispatched as ONE async unit: the whole fan-out runs- # on the daemon executor, joins on every child (see _execute_and_aggregate- # / dispatch_async_delegation_batch), and pushes a SINGLE completion event- # carrying the consolidated per-task results. It re-enters the conversation- # as one message once ALL children finish — the chat is not blocked while- # they run.+ # background applies to single tasks AND batches: a batch is ONE async unit+ # that joins on every child and re-enters as a single consolidated message.background = is_truthy_value(background, default=False) if background is not None else False- # Depth limit — configurable via delegation.max_spawn_depth,- # default 2 for parity with the original MAX_DEPTH constant.depth = getattr(parent_agent, "_delegate_depth", 0)max_spawn = _get_max_spawn_depth()if depth >= max_spawn:@@ -4006,14 +1098,10 @@ def delegate_task(f"multiplies API cost).")- # Load configcfg = _load_config()default_max_iter = cfg.get("max_iterations", DEFAULT_MAX_ITERATIONS)- # Model-supplied max_iterations is ignored — the config value is authoritative- # so users get predictable budgets. The kwarg is retained for internal callers- # and tests; a model-emitted value here would only shrink the budget and- # surprise the user mid-run. Log and drop it if one slips through from a- # cached tool schema or a stale provider.+ # Caller-supplied max_iterations is ignored: the config value is authoritative+ # so budgets stay predictable (kwarg kept for internal callers/tests).if max_iterations is not None and max_iterations != default_max_iter:logger.debug("delegate_task: ignoring caller-supplied max_iterations=%s; "@@ -4022,17 +1110,8 @@ def delegate_task()effective_max_iter = default_max_iter- # 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.- #- # ``credentials_cfg`` (internal callers only — never model-facing) is a- # per-call override shaped like the delegation config section- # ({provider, model, base_url, api_key, api_mode}); the /review engine- # uses it to route its reviewer subagent onto ``auxiliary.review``- # without touching the global delegation pin.+ # credentials_cfg (internal callers only, e.g. /review → auxiliary.review) is+ # a per-call override shaped like the delegation config section.try:creds = _resolve_delegation_credentials(credentials_cfg if credentials_cfg else cfg, parent_agent@@ -4040,7 +1119,6 @@ def delegate_task(except ValueError as exc:return tool_error(str(exc))- # Normalize to task listmax_children = _get_max_concurrent_children()recovered_tasks, tasks_error = _recover_tasks_from_json_string(tasks)if tasks_error:@@ -4048,10 +1126,7 @@ def delegate_task(if recovered_tasks is not None:tasks = recovered_tasks- # Small models frequently emit an empty tasks array ([]) alongside a- # single goal. Treat that as "no batch" instead of letting the batch- # quality gate below reject the goal-derived single task ("Batch mode- # requires at least 2 tasks") — the intent is unambiguous.+ # Small models emit tasks=[] alongside a single goal: treat as "no batch".if isinstance(tasks, list) and not tasks:tasks = None@@ -4080,7 +1155,6 @@ def delegate_task(if not task_list:return tool_error("No tasks provided.")- # Validate each task has a goalfor i, task in enumerate(task_list):if not isinstance(task, dict):return tool_error(@@ -4089,21 +1163,15 @@ def delegate_task(if not task.get("goal", "").strip():return tool_error(f"Task {i} is missing a 'goal'.")- # Batch-only quality gate: catch malformed fan-outs (placeholder goals,- # unexpanded multi-word template markers, 1-task batches) before any- # child is spawned. The single-`goal` form is deliberately exempt —- # short goals are valid there. Duplicate goals are allowed (best-of-N).- # Inspired by: MoonshotAI/kimi-code agent-swarm.md validation rules (MIT).+ # Batch-only quality gate (placeholders, template markers); the single-goal+ # form is exempt because short goals are valid there.if tasks is not None and isinstance(tasks, list):batch_error = _validate_batch_tasks(task_list)if batch_error:return tool_error(batch_error)- # T1-24: coerce/validate optional per-task output_schema up front so a- # malformed schema fails the whole call loudly instead of spawning- # children that can never satisfy their contract. Runs AFTER the- # existing goal checks; schema-less tasks resolve to None and take no- # new code paths downstream.+ # A malformed output_schema fails the whole call before any child spawns;+ # schema-less tasks resolve to None and take no new code paths downstream.from tools.delegation_output_schema import coerce_output_schematask_schemas: List[Optional[Dict[str, Any]]] = []@@ -4120,14 +1188,11 @@ def delegate_task(results = []n_tasks = len(task_list)- # Track goal labels for progress display (truncated for readability)task_labels = [t["goal"][:40] for t in task_list]- # Live transcripts: one pre-headered append-only log per task under- # cache/delegation/live/<delegation_id>/task-<n>.log so the caller can- # tail each child's operations while it runs (side-channel only — zero- # effect on message content or prompt caching). Best-effort: on failure- # live_paths is empty and delegation proceeds exactly as before.+ # Live transcripts: cache/delegation/live/<id>/task-<n>.log per task, a+ # side channel with zero effect on message content or prompt caching.+ # Best-effort: on failure live_paths is empty and delegation proceeds.from tools.delegation_live_log import (create_live_transcripts,update_manifest_statuses,@@ -4137,27 +1202,21 @@ def delegate_task(live_deleg_id, live_writers, live_paths = create_live_transcripts(task_list, context, model=creds.get("model"), provider=creds.get("provider"))- # Announce the batch tag once so the later ``[tag n/N]`` completion lines- # (and any nested batch's lines interleaving with them) are attributable.+ # Announce the batch tag once so interleaved ``[tag n/N]`` lines are attributable.if n_tasks > 1 and live_deleg_id:- _hdr = f"🔀 [{format_batch_tag(live_deleg_id)}] delegating {n_tasks} tasks"+ _hdr = f" 🔀 [{format_batch_tag(live_deleg_id)}] delegating {n_tasks} tasks"_hdr_spinner = getattr(parent_agent, "_delegate_spinner", None)if _hdr_spinner:try:- _hdr_spinner.print_above(f" {_hdr}")+ _hdr_spinner.print_above(_hdr)except Exception:- _emit_parent_console(parent_agent, f" {_hdr}")+ _emit_parent_console(parent_agent, _hdr)else:- _emit_parent_console(parent_agent, f" {_hdr}")-- # Capture the ORIGINATING session's wake target BEFORE any child agent is- # constructed: _build_child_agent() -> AIAgent() -> agent_init calls- # set_current_session_id(child.session_id), which clobbers the- # HERMES_SESSION_ID ContextVar and os.environ with the subagent's internal- # id before the background-dispatch code below would read it. The- # request-scoped chat_id binding (the raw X-Hermes-Session-Id on- # api_server) is untouched by child construction, so read it here and- # thread it through the dispatch.+ _emit_parent_console(parent_agent, _hdr)++ # Capture the ORIGINATING session's wake target BEFORE building any child:+ # AIAgent construction clobbers the HERMES_SESSION_ID ContextVar/os.environ+ # with the subagent's internal id.from tools.async_delegation import _current_origin_session_id_origin_wake_sid = _current_origin_session_id()@@ -4171,18 +1230,10 @@ def delegate_task(_capture_gateway_steer_authority(_origin_ui_session_id))- # Build all child agents on the main thread (thread-safe construction).- # _build_child_preserving_parent_tools saves/restores the parent's- # resolved tool names around each construction under a lock, so child- # toolset resolution never leaks into the parent (shared with the plugin- # subagent-lifecycle API).+ # Build every child on the main thread (construction is not thread-safe).children = []for i, t in enumerate(task_list):- # Per-task role beats top-level; normalise again so unknown- # per-task values warn and degrade to leaf uniformly.effective_role = _normalize_role(t.get("role") or top_role)- # T1-24: schema'd tasks get the contract appended to their context- # so the child knows the expected output shape before it starts._task_schema = task_schemas[i] if i < len(task_schemas) else None_child_context = t.get("context")if _task_schema is not None:@@ -4194,9 +1245,7 @@ def delegate_task(task_index=i,goal=t["goal"],context=_child_context,- # Subagents always inherit the parent's toolsets; the model- # cannot choose or narrow them (no model-facing toolsets arg).- toolsets=None,+ toolsets=None, # always inherit the parent's toolsetsmodel=creds["model"],max_iterations=effective_max_iter,task_count=n_tasks,@@ -4212,29 +1261,20 @@ def delegate_task(role=effective_role,)except ValueError as exc:- # Explicit-pin preflight failures (e.g. pinned delegation.command- # missing from PATH) refuse the spawn loudly (#80450).- return tool_error(str(exc))- # Attach the validated schema for the completion-side validation- # hook in _run_single_child. Absent (None) on schema-less tasks.+ return tool_error(str(exc)) # explicit-pin preflight failureif _task_schema is not None:try:child._delegate_output_schema = _task_schemaexcept Exception:logger.debug("Could not attach output schema to child %d", i)- # Tee the child's progress events into its live transcript log.- # wrap_progress_callback preserves the inner callback contract- # (including the _flush attribute) and never lets writer failures- # reach the agent loop. When no parent display exists the inner- # callback is None and the wrapper still records events.+ # Tee progress events into the live transcript (wrapper keeps the+ # _flush contract and swallows writer failures)._writer = live_writers[i] if i < len(live_writers) else Noneif _writer is not None:child.tool_progress_callback = wrap_progress_callback(getattr(child, "tool_progress_callback", None), _writer)child._live_transcript_path = str(_writer.path)- # Delegation identity for the live registry + process-notification- # attribution (child-started background processes report under it).if live_deleg_id:setattr(child, "_delegation_id", live_deleg_id)_ident_ref = getattr(child, "_progress_identity_ref", None)@@ -4243,17 +1283,13 @@ def delegate_task(children.append((i, t, child))def _execute_and_aggregate(*, honor_parent_interrupt: bool = True) -> dict:- """Run all built children (1 or N), join on them, aggregate results,- fire subagent_stop hooks + cost rollup, and return the combined result- dict. Used by BOTH the synchronous path and the background runner. In- the background case this whole function runs on the daemon executor, so- the parent turn isn't blocked — but the batch still JOINS on itself- here (all children must finish) before producing ONE consolidated- results block. That is the contract: fan-out runs in the background,- waits on each other, and returns together.+ """Run all built children, join, finalize (hooks + cost rollup), return the combined dict.++ Shared by the sync path and the background runner: even in the+ background the batch JOINS on itself here so ONE consolidated results+ block re-enters the conversation."""if n_tasks == 1:- # Single task -- run directly (no thread pool overhead)_i, _t, child = children[0]result = _run_single_child(_i,@@ -4266,159 +1302,26 @@ def delegate_task()results.append(result)else:- # Batch -- run in parallel with per-task progress lines- completed_count = 0- spinner_ref = getattr(parent_agent, "_delegate_spinner", None)-- # Daemon workers (tools.daemon_pool): the `with` block still joins- # normally, but if the parent is interrupted while a child is- # wedged, the abandoned worker must not block interpreter exit.- from tools.daemon_pool import DaemonThreadPoolExecutor- with DaemonThreadPoolExecutor(max_workers=max_children) as executor:- futures = {}- for i, t, child in children:- child_context = contextvars.copy_context()- future = executor.submit(- child_context.run,- _run_single_child,- task_index=i,- goal=t["goal"],- child=child,- parent_agent=parent_agent,- owner_session_id=_origin_ui_session_id or None,- owner_transport=_origin_owner_transport,- owner_session_record=_origin_owner_session_record,- )- futures[future] = i-- # Poll futures with interrupt checking. as_completed() blocks- # until ALL futures finish — if a child agent gets stuck,- # the parent blocks forever even after interrupt propagation.- # Instead, use wait() with a short timeout so we can bail- # when the parent is interrupted.- # Map task_index -> child agent, so fabricated entries for- # still-pending futures can carry the correct _delegate_role.- _child_by_index = {i: child for (i, _, child) in children}-- pending = set(futures.keys())- while pending:- if (- honor_parent_interrupt- and getattr(parent_agent, "_interrupt_requested", False) is True- ):- # Parent interrupted — collect whatever finished and- # abandon the rest. Children already received the- # interrupt signal; we just can't wait forever.- for f in pending:- idx = futures[f]- if f.done():- try:- entry = f.result()- except Exception as exc:- entry = {- "task_index": idx,- "status": "error",- "summary": None,- "error": str(exc),- "api_calls": 0,- "duration_seconds": 0,- "_child_role": getattr(- _child_by_index.get(idx), "_delegate_role", None- ),- }- else:- entry = {- "task_index": idx,- "status": "interrupted",- "summary": None,- "error": "Parent agent interrupted — child did not finish in time",- "api_calls": 0,- "duration_seconds": 0,- "_child_role": getattr(- _child_by_index.get(idx), "_delegate_role", None- ),- }- results.append(entry)- completed_count += 1- break-- from concurrent.futures import wait as _cf_wait, FIRST_COMPLETED-- done, pending = _cf_wait(- pending, timeout=0.5, return_when=FIRST_COMPLETED- )- for future in done:- try:- entry = future.result()- except Exception as exc:- idx = futures[future]- entry = {- "task_index": idx,- "status": "error",- "summary": None,- "error": str(exc),- "api_calls": 0,- "duration_seconds": 0,- "_child_role": getattr(- _child_by_index.get(idx), "_delegate_role", None- ),- }- results.append(entry)- completed_count += 1-- # Print per-task completion line above the spinner- 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- _tag = format_batch_tag(live_deleg_id)- _slot = f"{_tag} · {idx+1}/{n_tasks}" if _tag else f"{idx+1}/{n_tasks}"- completion_line = f"{icon} [{_slot}] {label} ({dur}s)"- # Failed/errored/timed-out children: say WHY on the- # same line, cleaned to one short human-readable- # fragment — a bare ✗ reads as "silently dropped".- if status in SUBAGENT_FAILURE_STATUSES:- _err_line = _clean_error_text(- entry.get("error"), max_chars=120- )- if _err_line:- completion_line += f" — {_err_line}"- if spinner_ref:- try:- spinner_ref.print_above(completion_line)- except Exception:- _emit_parent_console(parent_agent, f" {completion_line}")- else:- _emit_parent_console(parent_agent, f" {completion_line}")-- # Update spinner text to show remaining count- if spinner_ref and remaining > 0:- try:- spinner_ref.update_text(- f"🔀 {'[' + _tag + '] ' if _tag else ''}{remaining} task{'s' if remaining != 1 else ''} remaining"- )- except Exception as e:- logger.debug("Spinner update_text failed: %s", e)-- # Sort by task_index so results match input order- results.sort(key=lambda r: r["task_index"])-- # Cap subagent summaries against the parent's remaining context- # headroom (split across the batch) before they enter the parent's- # conversation. Full text is spilled to disk so nothing is lost.- # Covers both the single-task and batch paths. See PR #9126.+ _run_children_parallel(+ children,+ results,+ parent_agent=parent_agent,+ n_tasks=n_tasks,+ max_children=max_children,+ task_labels=task_labels,+ live_deleg_id=live_deleg_id,+ honor_parent_interrupt=honor_parent_interrupt,+ origin_ui_session_id=_origin_ui_session_id,+ origin_owner_transport=_origin_owner_transport,+ origin_owner_session_record=_origin_owner_session_record,+ )+_finalize_child_results(results, task_list, children, parent_agent)total_duration = round(time.monotonic() - overall_start, 2)- # Close out the live transcripts: terminal marker per task + manifest- # status update. The files are retained (retention pruning happens on- # future dispatches) — they double as the full-fidelity operational- # record alongside the summary spill files.+ # Close out live transcripts (files are retained as the full-fidelity+ # record; retention pruning happens on future dispatches).for entry in results:_idx = entry.get("task_index", -1)_w = (@@ -4443,672 +1346,41 @@ def delegate_task(combined["live_transcripts"] = list(live_paths)return combined- # ----- Background dispatch: run the WHOLE batch as one async unit ------ # When background is true, the entire fan-out runs on the daemon executor- # via a single async delegation. _execute_and_aggregate() joins on every- # child and produces ONE consolidated results block, which re-enters the- # conversation as a single message when ALL children finish. The chat is- # not blocked in the meantime. This is the contract: dispatch N subagents,- # keep chatting, get the combined summaries back together at the end.if background:- from tools.async_delegation import dispatch_async_delegation_batch- from tools.approval import get_current_session_key-- # Finite sessions cannot route a detached subagent result back to the- # agent after their turn/process ends. This includes stateless HTTP- # requests (#10760) and one-shot Kanban workers (#63169). Fall back to- # SYNCHRONOUS execution so the result returns in this same turn instead- # of handing out a handle with no durable consumer. Mirrors the- # pool-at-capacity inline fallback below.- try:- from gateway.session_context import async_delivery_supported- _async_ok = async_delivery_supported()- except Exception:- _async_ok = True-- _wake_sid = ""- if not _async_ok:- # The adapter itself cannot push, but if a raw session id is- # bound (the API server always binds one — see- # ApiServerAdapter._bind_api_server_session), gateway.wake can- # still reach the session by self-POSTing /v1/chat/completions- # with that id in X-Hermes-Session-Id once the batch completes.- # Only fall back to forced-sync execution when there is truly no- # session id to wake. Uses the origin captured before child- # construction (see _origin_wake_sid above) — reading- # HERMES_SESSION_ID here would return the subagent's internal id.- _wake_sid = _origin_wake_sid- if _wake_sid:- logger.info(- "delegate_task: async delivery unsupported on this "- "session, but a session id is bound (%s) — dispatching "- "in the background and waking the session via self-post "- "when it completes instead of forcing synchronous "- "execution.",- _wake_sid,- )- _async_ok = True-- if not _async_ok:- logger.info(- "delegate_task: async delivery unsupported on this session "- "runtime; running the batch synchronously instead."- )- _sync_result = _execute_and_aggregate()- if isinstance(_sync_result, dict):- _sync_result["note"] = (- "background=true is not available in this session — it cannot "- "receive a detached subagent result after the turn ends (a "- "one-shot runner such as `hermes -z`, a cron job, a Kanban "- "worker, or a stateless HTTP endpoint). The subagent(s) ran "- "SYNCHRONOUSLY and the result is included above."- )- return json.dumps(_sync_result, ensure_ascii=False)-- _session_key = get_current_session_key(default="")- try:- from gateway.session_context import get_session_env-- _source = get_session_env("HERMES_SESSION_SOURCE", "")- # Refresh from the same task-local source when available, but retain- # the immutable value captured before child construction otherwise.- _origin_ui_session_id = (- get_session_env("HERMES_UI_SESSION_ID", "") or _origin_ui_session_id- )- # In desktop/TUI, the routable session key is the durable- # AIAgent.session_id. Context compression can rotate that id during- # the same turn before the TUI-side session dict is re-anchored;- # if we capture the stale approval/session context key here, the- # async completion becomes an orphan and any desktop poller may- # consume it. Gateway chats are different: their session_key is the- # platform conversation key (agent:main:...), so keep it there.- if _source == "tui":- _agent_session_id = str(getattr(parent_agent, "session_id", "") or "")- if _agent_session_id:- _session_key = _agent_session_id- except Exception:- _source = ""- if not _session_key:- # CLI (single-process) path: the approval contextvar is only bound- # during gateway/TUI turns and HERMES_SESSION_KEY is not in the CLI- # environment, so the key resolves empty here. Since #64240 the CLI- # drains completions through a positive-ownership filter keyed on- # the durable AIAgent.session_id — an empty session_key would fail- # closed and the CLI could never claim its own completions, while- # a restored foreign event with an empty key could leak into any- # unfiltered consumer (#64484). Stamp the parent's durable session- # id instead; compression rotations are handled on the drain side- # via resolve_resume_session_id lineage resolution.- _agent_session_id = str(getattr(parent_agent, "session_id", "") or "")- if _agent_session_id:- _session_key = _agent_session_id- _parent_session_id = getattr(parent_agent, "session_id", None)- _child_agents = [c for (_, _, c) in children]-- # Detach every child from the parent's interrupt-propagation list — the- # batch's lifecycle is owned by the async registry now, not the parent- # turn. _build_child_agent attached them (correct for sync runs).- if hasattr(parent_agent, "_active_children"):- _ac_lock = getattr(parent_agent, "_active_children_lock", None)- for _c in _child_agents:- try:- if _ac_lock:- with _ac_lock:- parent_agent._active_children.remove(_c)- else:- parent_agent._active_children.remove(_c)- except ValueError:- pass-- def _batch_runner():- # This batch is detached from the foreground turn. Its lifecycle is- # owned by the async registry and cancelled only via _batch_interrupt.- return _execute_and_aggregate(honor_parent_interrupt=False)-- def _batch_interrupt():- for _c in _child_agents:- try:- interrupted = request_hard_interrupt(_c, "Async delegation cancelled")- if not interrupted and hasattr(_c, "_interrupt_requested"):- _c._interrupt_requested = True- except Exception:- pass-- def _batch_progress():- # Progress token for the async registry's stale monitor: the- # combined (api_call_count, current_tool, last_activity_ts) of- # every child. last_activity_ts is ticked by _touch_activity on- # every streamed chunk ("receiving stream response"), every tool- # transition, and every API-call start/completion — so a child- # streaming a long response is alive even though api_call_count- # only advances when the call completes (same liveness signal as- # the compaction inactivity budget, PR #71508). A fully frozen- # token past the stale threshold means the detached batch is- # wedged (e.g. stuck inside the first model API call — #60203).- # in_tool=True while ANY child is inside a tool so legitimately- # slow tools get the higher staleness ceiling, mirroring the- # sync-path heartbeat monitor.- parts = []- in_tool = False- for _c in _child_agents:- try:- _summary = _c.get_activity_summary()- _tool = _summary.get("current_tool")- parts.append(- (- _summary.get("api_call_count", 0),- _tool,- _summary.get("last_activity_ts"),- )- )- in_tool = in_tool or bool(_tool)- except Exception:- parts.append(None)- return tuple(parts), in_tool-- _goals = [t["goal"] for t in task_list]- dispatch = dispatch_async_delegation_batch(- goals=_goals,+ return _dispatch_background(+ parent_agent=parent_agent,context=context,- # Metadata for the completion block only; subagents inherit the- # parent's toolsets (no model-facing toolsets arg).- toolsets=None,- role=top_role,- model=creds["model"],- session_key=_session_key,+ task_list=task_list,+ children=children,+ creds=creds,+ top_role=top_role,+ live_deleg_id=live_deleg_id,+ live_paths=live_paths,+ origin_wake_sid=_origin_wake_sid,origin_ui_session_id=_origin_ui_session_id,- origin_session_id=_wake_sid,- parent_session_id=_parent_session_id,- runner=_batch_runner,- interrupt_fn=_batch_interrupt,- max_async_children=_get_max_async_children(),- # Reuse the live-transcript directory's id (when created) so the- # returned delegation_id matches cache/delegation/live/<id>/.- delegation_id=live_deleg_id,- progress_fn=_batch_progress,+ execute_and_aggregate=_execute_and_aggregate,)- if dispatch.get("status") == "dispatched":- n = len(_goals)- note = (- "Subagent is running in the background. You and the user can "- "keep working; its full result re-enters the conversation as a "- "new message when it finishes. Do not wait or poll — just "- "continue."- if n == 1 else- f"{n} subagents are running in parallel in the background. You "- f"and the user can keep working; they wait on each other and "- f"their consolidated results re-enter the conversation as a "- f"single message once ALL of them finish. Do not wait or poll "- f"— just continue."- )- payload = {- "status": "dispatched",- "mode": "background",- "count": n,- "delegation_id": dispatch["delegation_id"],- "goals": _goals,- "note": note,- }- _sids = [- getattr(_c, "_subagent_id", None) for _c in _child_agents- ]- if any(isinstance(s, str) and s for s in _sids):- payload["subagent_ids"] = _sids- payload["control_hint"] = (- "While a child runs you can orchestrate it live with this "- "same tool: delegate_task(action='list') to see live "- "children, action='steer' with subagent_id + message to "- "redirect one, action='stop' with subagent_id to end one "- "early."- )- if live_paths:- payload["live_transcripts"] = list(live_paths)- payload["live_transcripts_hint"] = (- "Each subagent streams a human-readable transcript of its "- "operations to the file listed above (append-only, one per "- "task). Read or `tail -f` these paths at any time to watch "- "a child work while it runs."- )- return json.dumps(payload, ensure_ascii=False)-- # Pool at capacity / schedule failure — children are still attached- # (we detach above only on the parent list, but the async unit was- # never accepted, so re-attaching isn't needed: we just run inline).- logger.info(- "delegate_task: async pool at capacity (%s); running the whole "- "batch synchronously instead.",- dispatch.get("error", "rejected"),- )- _cap_result = _execute_and_aggregate()- if isinstance(_cap_result, dict):- _cap_result["note"] = (- "The background delegation pool was at capacity "- "(delegation.max_concurrent_children), so the subagent(s) ran "- "SYNCHRONOUSLY and the result is included above. Raise "- "delegation.max_concurrent_children in config.yaml to allow "- "more concurrent background delegations."- )- return json.dumps(_cap_result, ensure_ascii=False)-- # ----- Synchronous path -----return json.dumps(_execute_and_aggregate(), ensure_ascii=False)-def _resolve_child_credential_pool(- effective_provider: Optional[str],- parent_agent,- effective_base_url: Optional[str] = None,-):- """Resolve a credential pool for the child agent.-- Rules:- 1. Same provider as the parent -> share the parent's pool so cooldown state- and rotation stay synchronized.- 2. Different provider -> try to load that provider's own pool.- 3. No pool available -> return None and let the child keep the inherited- fixed credential behavior.-- Custom endpoints are a special case: every direct ``delegation.base_url``- runtime collapses to ``provider="custom"``, so bare provider equality would- treat two *different* custom endpoints as interchangeable and let the child- inherit the parent's pool. Leasing from that pool then overwrites the- child's delegated ``base_url`` with the parent's endpoint (issue #7833).- We therefore resolve custom runtimes by endpoint identity (the- ``custom:<name>`` pool key derived from the base_url) and only share the- parent's pool when both resolve to the *same* custom endpoint.- """- if not effective_provider:- return getattr(parent_agent, "_credential_pool", None)-- parent_provider = getattr(parent_agent, "provider", None) or ""- parent_pool = getattr(parent_agent, "_credential_pool", None)-- # Custom endpoints: distinguish by endpoint identity, not the bare "custom"- # provider string. Two custom runtimes are only interchangeable when they- # resolve to the same custom:<name> pool key.- if effective_provider == "custom":- try:- from agent.credential_pool import get_custom_provider_pool_key, load_pool-- child_key = get_custom_provider_pool_key(effective_base_url)- if child_key is None:- # Unregistered endpoint (raw delegation.base_url with no- # matching custom_providers entry) -> no shared pool exists.- # Keep the child's fixed delegated credential rather than- # risk inheriting the parent's custom endpoint.- return None-- # Reuse the parent's pool only when it is the same custom endpoint.- parent_key = get_custom_provider_pool_key(- getattr(parent_agent, "base_url", None)- )- if (- parent_pool is not None- and parent_provider == "custom"- and parent_key is not None- and parent_key == child_key- ):- return parent_pool-- pool = load_pool(child_key)- if pool is not None and pool.has_credentials():- return pool- except Exception as exc:- logger.debug(- "Could not resolve custom credential pool for child endpoint '%s': %s",- effective_base_url,- exc,- )- return None-- if parent_pool is not None and effective_provider == parent_provider:- return parent_pool-- try:- from agent.credential_pool import load_pool-- pool = load_pool(effective_provider)- if pool is not None and pool.has_credentials():- return pool- except Exception as exc:- logger.debug(- "Could not load credential pool for child provider '%s': %s",- effective_provider,- exc,- )- return None---def _merge_request_overrides(runtime_overrides, explicit_overrides):- """Merge explicit ``delegation.request_overrides`` over runtime-derived ones.-- Precedence contract: the explicit config key WINS over runtime-derived- (provider-catalog or parent-inherited) overrides. Top-level keys from the- explicit dict replace same-named runtime keys; the ``extra_body`` sub-dict- is deep-merged ONE level — runtime ``extra_body`` keys survive unless the- explicit dict redefines that exact key. This keeps provider personality- (e.g. ``thinking: {type: disabled}``) intact while letting users layer- routing hints (e.g. ``extra_body.provider = {"sort": "throughput"}``) on- top.-- Both inputs are deep-copied (``copy.deepcopy``) so transport-side mutation- of the child's request kwargs can never leak back into the loaded config- dict or the provider runtime cache.-- Returns ``None`` when both sides are empty/non-dict.- """- import copy as _copy-- runtime_overrides = runtime_overrides if isinstance(runtime_overrides, dict) else None- explicit_overrides = explicit_overrides if isinstance(explicit_overrides, dict) else None- if not runtime_overrides and not explicit_overrides:- return None- merged = _copy.deepcopy(runtime_overrides) if runtime_overrides else {}- explicit = _copy.deepcopy(explicit_overrides) if explicit_overrides else {}- runtime_extra = merged.get("extra_body")- explicit_extra = explicit.pop("extra_body", None)- merged.update(explicit)- if isinstance(runtime_extra, dict) and isinstance(explicit_extra, dict):- runtime_extra.update(explicit_extra)- merged["extra_body"] = runtime_extra- elif explicit_extra is not None:- merged["extra_body"] = explicit_extra- return merged or None---def _resolve_delegation_credentials(cfg: dict, parent_agent) -> dict:- """Resolve credentials for subagent delegation.-- If ``delegation.base_url`` is configured, subagents use that direct- OpenAI-compatible endpoint. ``delegation.api_key`` overrides the key; when- omitted, ``api_key`` is returned as ``None`` so ``_build_child_agent``- inherits the parent agent's key (``effective_api_key = override_api_key or- parent_api_key``). This lets providers that store their key outside- ``OPENAI_API_KEY`` (e.g. ``MINIMAX_API_KEY``, ``DASHSCOPE_API_KEY``) work- without a duplicate config entry.-- Otherwise, if ``delegation.provider`` is configured, the full credential- bundle (base_url, api_key, api_mode, provider) is resolved 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 neither base_url nor 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 = str(cfg.get("model") or "").strip() or None- configured_provider = str(cfg.get("provider") or "").strip() or None- configured_base_url = str(cfg.get("base_url") or "").strip() or None- configured_api_key = str(cfg.get("api_key") or "").strip() or None- configured_api_mode = str(cfg.get("api_mode") or "").strip().lower() or None-- # delegation.request_overrides: explicit per-child request settings from- # config. Honored on EVERY resolution branch (direct base_url, named- # provider, and parent-inherit) so the key never silently no-ops.- # Precedence: explicit merges OVER runtime/parent-derived overrides via- # _merge_request_overrides (top-level explicit keys win; extra_body is- # deep-merged one level). Non-dict values are ignored.- explicit_request_overrides = (- cfg.get("request_overrides")- if isinstance(cfg.get("request_overrides"), dict)- else None- )-- # Native-SDK providers (Bedrock, Vertex, Google GenAI) speak their own- # wire protocol — they cannot be reached via OpenAI chat_completions against- # a base_url. For these, always fall through to resolve_runtime_provider()- # so the proper SDK path is taken. The configured base_url is still- # forwarded through runtime-provider resolution when applicable (e.g. a- # custom Bedrock regional endpoint).- _NATIVE_SDK_PROVIDERS = {"bedrock", "vertex", "google", "google-genai"}- _provider_lower = (configured_provider or "").strip().lower()- _is_native_sdk_provider = _provider_lower in _NATIVE_SDK_PROVIDERS-- if configured_base_url and not _is_native_sdk_provider:- # delegation.request_overrides: an explicit dict of per-child request- # settings merged into the child's API kwargs by the transport's- # profile path. Keys are top-level kwargs (e.g. service_tier); an- # "extra_body" sub-dict is merged into extra_body. This is how a- # direct-endpoint delegation (provider=custom) forwards OpenRouter- # routing hints such as extra_body.provider = {"sort": "throughput"}- # to its children — the child's CustomProfile does not emit provider- # preferences, and the parent-inheritance path is deliberately cleared- # when delegation.provider/base_url overrides the parent (see the- # provider-preference clearing in _build_child_agent).- #- # Precedence: explicit delegation.request_overrides MERGES OVER any- # runtime-derived overrides (see _merge_request_overrides) — top-level- # explicit keys win; extra_body is deep-merged one level so runtime- # extra_body keys survive unless the explicit key redefines them.- # (explicit_request_overrides is parsed once at the top of this- # function and applied to every branch.)-- # When delegation.api_key is not set, return None so _build_child_agent- # falls back to the parent agent's API key via the credential inheritance- # path (effective_api_key = override_api_key or parent_api_key). This- # lets providers that store their key in a non-OPENAI_API_KEY env var- # (e.g. MINIMAX_API_KEY, DASHSCOPE_API_KEY) work without requiring- # callers to duplicate the key under delegation.api_key.- api_key = configured_api_key # None → inherited from parent in _build_child_agent-- # Use the shared URL-based api_mode detector (same path the main agent's- # runtime resolver uses) so Anthropic-compatible direct endpoints with a- # /anthropic suffix — Azure AI Foundry, MiniMax, Zhipu GLM, LiteLLM- # proxies — pick the right transport automatically. Without this,- # subagents would default to chat_completions and hit 404s on endpoints- # that only speak the Anthropic Messages protocol. Fixes #10213.- from hermes_cli.runtime_provider import _detect_api_mode_for_url-- base_lower = configured_base_url.lower()- provider = "custom"- api_mode = _detect_api_mode_for_url(configured_base_url) or "chat_completions"- if (- base_url_hostname(configured_base_url) == "chatgpt.com"- and "/backend-api/codex" in base_lower- ):- provider = "openai-codex"- api_mode = "codex_responses"- elif base_url_hostname(configured_base_url) == "api.anthropic.com":- provider = "anthropic"- api_mode = "anthropic_messages"- elif "api.kimi.com/coding" in base_lower:- provider = "custom"- api_mode = "anthropic_messages"-- # Explicit delegation.api_mode in config always wins. Lets users force- # a transport for non-standard endpoints the URL heuristic can't detect.- if configured_api_mode in {"chat_completions", "codex_responses", "anthropic_messages"}:- api_mode = configured_api_mode-- # A provider configured ALONGSIDE base_url means the user wants that- # provider's request personality on an explicit endpoint. This- # short-circuit runs before the resolve_runtime_provider() call below,- # so without this block the runtime-carried request_overrides- # (extra_body / extra_headers, e.g. `thinking: {type: disabled}`) and- # max_output_tokens are silently dropped for subagents (#65035).- # Best-effort: the explicit endpoint worked before this change even- # when the provider can't resolve, so a resolution failure only skips- # the overrides — it must not fail the dispatch.- request_overrides = None- max_output_tokens = None- if configured_provider:- try:- from hermes_cli.runtime_provider import resolve_runtime_provider-- runtime = resolve_runtime_provider(- requested=configured_provider, target_model=configured_model- )- request_overrides = dict(runtime.get("request_overrides") or {}) or None- max_output_tokens = runtime.get("max_output_tokens")- except Exception as exc:- logger.debug(- "delegation.base_url: runtime resolution for provider '%s' "- "failed; proceeding without request_overrides: %s",- configured_provider,- exc,- )-- # Explicit delegation.request_overrides merges OVER the runtime-derived- # overrides (explicit wins; extra_body deep-merged one level).- request_overrides = _merge_request_overrides(- request_overrides, explicit_request_overrides- )-- return {- "model": configured_model,- "provider": provider,- "base_url": configured_base_url,- "api_key": api_key,- "api_mode": api_mode,- "request_overrides": request_overrides,- "max_output_tokens": max_output_tokens,- }-- if not configured_provider:- # No provider override — child inherits everything from parent.- # delegation.request_overrides still applies: merge the explicit key- # OVER the parent's own request_overrides so the config key works even- # in pure-inherit setups (never a silent no-op). None when neither- # side has values → _build_child_agent falls back to the parent's- # request_overrides unchanged.- return {- "model": configured_model,- "provider": None,- "base_url": None,- "api_key": None,- "api_mode": None,- "request_overrides": _merge_request_overrides(- getattr(parent_agent, "request_overrides", None),- explicit_request_overrides,- ),- "max_output_tokens": None,- }-- # Provider is configured — resolve full credentials- try:- from hermes_cli.runtime_provider import resolve_runtime_provider-- runtime = resolve_runtime_provider(requested=configured_provider, target_model=configured_model)- 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"or set delegation.base_url/delegation.api_key for a direct endpoint. "- 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 auth'."- )-- # A pinned ACP transport command must exist — refuse the spawn loudly- # rather than letting the child silently fall back to another transport- # (#80450).- pinned_command = runtime.get("command")- if pinned_command:- import shutil as _shutil-- if not _shutil.which(pinned_command):- raise ValueError(- f"Delegation provider '{configured_provider}' is pinned to the "- f"'{pinned_command}' command, which was not found on PATH. "- f"Install it or choose a different delegation provider."- )-- return {- "model": configured_model or runtime.get("model") or None,- "provider": configured_provider if runtime.get("provider") == _RUNTIME_PROVIDER_CUSTOM else runtime.get("provider"),- "base_url": runtime.get("base_url"),- "api_key": api_key,- "api_mode": runtime.get("api_mode"),- # Explicit delegation.request_overrides merges OVER the named- # provider's runtime overrides (explicit wins; extra_body deep-merged- # one level) — same precedence as the direct-base_url branch above.- "request_overrides": _merge_request_overrides(- runtime.get("request_overrides"), explicit_request_overrides- )- or {},- "max_output_tokens": runtime.get("max_output_tokens"),- "command": runtime.get("command"),- "args": list(runtime.get("args") or []),- }---def _load_config() -> dict:- """Load delegation config from the active Hermes config.-- Prefer the shared persistent loader because it follows the active- HERMES_HOME/profile. ``cli.CLI_CONFIG`` is a legacy fallback for entry- points that cannot import the shared loader; importing it first can return- an old default ``delegation`` block and hide user-set keys such as- ``max_concurrent_children``.-- Uses ``load_config_readonly()``: every consumer of this dict is read-only- (``.get()`` lookups), and this runs on each ``get_definitions()`` schema- rebuild via ``_get_max_concurrent_children``, so skipping the defensive- deepcopy matters. Do NOT mutate the returned dict.-- ``HERMES_IGNORE_USER_CONFIG=1`` (``hermes chat --ignore-user-config``) is- only honored by the legacy ``cli`` loader, not the shared one, so when the- flag is set we keep ``cli.CLI_CONFIG`` authoritative to preserve the- flag's contract of suppressing user config.yaml settings.- """- prefer_legacy = os.environ.get("HERMES_IGNORE_USER_CONFIG") == "1"- if not prefer_legacy:- try:- from hermes_cli.config import load_config_readonly-- full = load_config_readonly()- cfg = full.get("delegation") or {}- if isinstance(cfg, dict):- return cfg- except Exception:- pass- try:- from cli import CLI_CONFIG-- cfg = CLI_CONFIG.get("delegation") or {}- return cfg if isinstance(cfg, dict) else {}- except Exception:- return {}---# ----------------------------------------------------------------------------# OpenAI Function-Calling Schema-# ---------------------------------------------------------------------------+# ── OpenAI function-calling schema ──────────────────────────────────────────def _build_top_level_description() -> str:"""Compose the delegate_task tool description.- Deliberately carries ONLY guidance that exists nowhere else in the- schema. Batch/concurrency limits live in the 'tasks' parameter- description and the nesting clause lives in the 'role' parameter- description (both rebuilt per get_definitions() call with the user's- actual delegation.max_concurrent_children / max_spawn_depth), so the- top-level text stays static and duplication-free. If you add text- here, check it is not already stated in a parameter description.+ Carries ONLY guidance stated nowhere else in the schema (limits live in+ the 'tasks' parameter description, rebuilt per get_definitions() call)."""try:orchestration_available = _get_max_spawn_depth() >= 2 and _get_orchestrator_enabled()except Exception:orchestration_available = False- # The child-restrictions rule renders per config: on nesting-enabled- # installs the orchestrator clause is load-bearing; on depth-1/disabled- # installs (the default) it would describe an unreachable state — the- # role param already explains that 'orchestrator' is inert there.- # send_message is deliberately not named: it's gateway-internal- # vocabulary most sessions never see. The list below is the fail-safe- # superset; model_tools session-filters it to the tools the session- # actually has, dropping the whole line when none apply.- # Delegation capability is depth-derived (no role param): mention- # recursion only where it's actually available.+ # Mention recursion only where it's actually available. send_message is+ # deliberately not named (gateway-internal vocabulary); model_tools+ # session-filters the list to tools the session has.if orchestration_available:restrictions_rule = ("- Children cannot call clarify, memory, or cronjob.\n"@@ -5171,27 +1443,6 @@ def _build_tasks_param_description() -> str:)-def _build_role_param_description() -> str:- """Legacy helper — the `role` param is no longer advertised.-- Delegation capability is depth-derived (see the role-resolution block in- _build_child_agent): a child may itself delegate iff- delegation.orchestrator_enabled and its depth < max_spawn_depth. The- handler still accepts role for wire compat (old transcripts, kanban- dispatcher) but ignores it. Kept because external callers import this- symbol; returns the depth story for any such use.- """- try:- max_depth = _get_max_spawn_depth()- except Exception:- max_depth = MAX_DEPTH- return (- "Legacy parameter, ignored: whether a child can delegate is derived "- f"from delegation config (max_spawn_depth={max_depth}), not declared "- "by the caller."- )--def _build_dynamic_schema_overrides() -> dict:"""Return per-call schema overrides reflecting current config.diff --git a/tools/delegate_tool_child_run.py b/tools/delegate_tool_child_run.pynew file mode 100644index 00000000000..89c6093d03f--- /dev/null+++ b/tools/delegate_tool_child_run.py@@ -0,0 +1,1177 @@+"""Helpers for running ONE pre-built child agent: heartbeat, registry entry, workspace seeding, timeout/failure handling, result-entry assembly and cleanup.++Split out of ``tools/delegate_tool.py``; every moved name is re-imported there, so+``tools.delegate_tool.<name>`` keeps resolving (and monkeypatching) as before.+"""++from __future__ import annotations++import logging+import json+import threading+import time+from concurrent.futures import TimeoutError as FuturesTimeoutError+from typing import Any, Dict, List, Optional+from agent.interrupt_compat import request_hard_interrupt+from dataclasses import dataclass+from tools import file_state+from tools.delegate_tool_registry import (+ _capture_gateway_steer_authority, _close_subagent_steering, _register_subagent, _unregister_subagent,+)+from tools.delegate_tool_results import (+ _extract_output_tail, _looks_like_error_output, _stringify_tool_content, _summarize_tool_arguments,+)++# Log-record parity with the origin module.+logger = logging.getLogger("tools.delegate_tool")+++def _dump_subagent_timeout_diagnostic(+ *,+ child: Any,+ task_index: int,+ timeout_seconds: float,+ duration_seconds: float,+ worker_thread: Optional[threading.Thread],+ goal: str,+) -> Optional[str]:+ """Write a structured diagnostic dump for a subagent that timed out+ before making any API call.++ See issue #14726: users hit "subagent timed out after 300s with no response"+ with zero API calls and no way to inspect what happened. This helper+ writes a dedicated log under ``~/.hermes/logs/subagent-<sid>-<ts>.log``+ capturing the child's config, system-prompt / tool-schema sizes, activity+ tracker snapshot, and the worker thread's Python stack at timeout.++ Returns the absolute path to the diagnostic file, or None on failure.+ """+ try:+ from hermes_constants import get_hermes_home+ import datetime as _dt+ import sys as _sys+ import traceback as _traceback+ import threading as _threading++ hermes_home = get_hermes_home()+ logs_dir = hermes_home / "logs"+ try:+ logs_dir.mkdir(parents=True, exist_ok=True)+ except Exception:+ return None++ subagent_id = getattr(child, "_subagent_id", None) or f"idx{task_index}"+ ts = _dt.datetime.now().strftime("%Y%m%d_%H%M%S")+ dump_path = logs_dir / f"subagent-timeout-{subagent_id}-{ts}.log"++ lines: List[str] = []+ def _w(line: str = "") -> None:+ lines.append(line)++ _w("# Subagent timeout diagnostic — issue #14726")+ _w(f"# Generated: {_dt.datetime.now().isoformat()}")+ _w("")+ _w("## Timeout")+ _w(f" task_index: {task_index}")+ _w(f" subagent_id: {subagent_id}")+ _w(f" configured_timeout: {timeout_seconds}s")+ _w(f" actual_duration: {duration_seconds:.2f}s")+ _w("")++ _w("## Goal")+ _goal_preview = (goal or "").strip()+ if len(_goal_preview) > 1000:+ _goal_preview = _goal_preview[:1000] + " ...[truncated]"+ _w(_goal_preview or "(empty)")+ _w("")++ _w("## Child config")+ for attr in (+ "model", "provider", "api_mode", "base_url", "max_iterations",+ "quiet_mode", "skip_memory", "skip_context_files", "platform",+ "_delegate_role", "_delegate_depth",+ ):+ try:+ val = getattr(child, attr, None)+ # Redact api_key-shaped values defensively+ if isinstance(val, str) and attr == "base_url":+ pass+ _w(f" {attr}: {val!r}")+ except Exception:+ _w(f" {attr}: <unreadable>")+ _w("")++ _w("## Toolsets")+ enabled = getattr(child, "enabled_toolsets", None)+ _w(f" enabled_toolsets: {enabled!r}")+ tool_names = getattr(child, "valid_tool_names", None)+ if tool_names:+ _w(f" loaded tool count: {len(tool_names)}")+ try:+ _w(f" loaded tools: {sorted(tool_names)}")+ except Exception:+ pass+ _w("")++ _w("## Prompt / schema sizes")+ try:+ sys_prompt = getattr(child, "ephemeral_system_prompt", None) \+ or getattr(child, "system_prompt", None) \+ or ""+ _w(f" system_prompt_bytes: {len(sys_prompt.encode('utf-8')) if isinstance(sys_prompt, str) else 'n/a'}")+ _w(f" system_prompt_chars: {len(sys_prompt) if isinstance(sys_prompt, str) else 'n/a'}")+ except Exception as exc:+ _w(f" system_prompt: <error: {exc}>")+ try:+ tools_schema = getattr(child, "tools", None)+ if tools_schema is not None:+ _schema_json = json.dumps(tools_schema, default=str)+ _w(f" tool_schema_count: {len(tools_schema)}")+ _w(f" tool_schema_bytes: {len(_schema_json.encode('utf-8'))}")+ except Exception as exc:+ _w(f" tool_schema: <error: {exc}>")+ _w("")++ _w("## Activity summary")+ try:+ summary = child.get_activity_summary()+ for k, v in summary.items():+ _w(f" {k}: {v!r}")+ except Exception as exc:+ _w(f" <get_activity_summary failed: {exc}>")+ _w("")++ _w("## Worker thread stack at timeout")+ if worker_thread is not None and worker_thread.is_alive():+ frames = _sys._current_frames()+ worker_frame = frames.get(worker_thread.ident)+ if worker_frame is not None:+ stack = _traceback.format_stack(worker_frame)+ for frame_line in stack:+ for sub in frame_line.rstrip().split("\n"):+ _w(f" {sub}")+ else:+ _w(" <worker frame not available>")+ elif worker_thread is None:+ _w(" <no worker thread handle>")+ else:+ _w(" <worker thread already exited>")+ _w("")++ # All other live threads (bounded to 40): the worker is often parked on+ # a helper thread, so a pre-HTTP wedge is indistinguishable from a slow+ # provider without the full picture.+ _w("## All thread stacks at timeout")+ try:+ frames = _sys._current_frames()+ by_ident = {+ th.ident: th for th in _threading.enumerate() if th.ident+ }+ worker_ident = worker_thread.ident if worker_thread else None+ dumped = 0+ for ident, frame in frames.items():+ if ident == worker_ident:+ continue # already dumped above+ if dumped >= 40:+ _w(f" <{len(frames) - dumped - 1} more threads omitted>")+ break+ th = by_ident.get(ident)+ name = th.name if th else f"ident={ident}"+ daemon = " daemon" if (th and th.daemon) else ""+ _w(f" --- {name}{daemon} ---")+ for frame_line in _traceback.format_stack(frame):+ for sub in frame_line.rstrip().split("\n"):+ _w(f" {sub}")+ dumped += 1+ except Exception as exc:+ _w(f" <all-thread dump failed: {exc}>")+ _w("")++ _w("## Notes")+ _w(" This file is written ONLY when a subagent times out with 0 API calls.")+ _w(" 0-API-call timeouts mean the child never reached its first LLM request.")+ _w(" Common causes: oversized prompt rejected by provider, transport hang,")+ _w(" credential resolution stuck. See issue #14726 for context.")++ dump_path.write_text("\n".join(lines), encoding="utf-8")+ return str(dump_path)+ except Exception as exc:+ logger.warning("Subagent timeout diagnostic dump failed: %s", exc)+ return None+++def _start_heartbeat(child: Any, parent_agent: Any, task_index: int) -> tuple:+ """Build the parent-activity heartbeat thread for one child (not started).++ Returns ``(stop_event, thread)``. The caller starts the thread inside its+ ``try`` so a failed ``start()`` (OS thread exhaustion) leaves ``ident`` None+ and the finally-path join can be skipped safely.+ """+ from tools.delegate_tool import (+ _HEARTBEAT_INTERVAL,+ _HEARTBEAT_STALE_CYCLES_IDLE,+ _HEARTBEAT_STALE_CYCLES_IN_TOOL,+ )++ _heartbeat_stop = threading.Event()+ # Stale detection: a cycle counts as stale when (tool, iteration,+ # activity_ts) all froze; thresholds differ idle vs in-tool.+ _last_seen_iter = [0]+ _last_seen_tool = [None] # type: list+ _last_seen_activity_ts = [None] # type: list+ _stale_count = [0]++ def _heartbeat_loop():+ while not _heartbeat_stop.wait(_HEARTBEAT_INTERVAL):+ if parent_agent is None:+ continue+ touch = getattr(parent_agent, "_touch_activity", None)+ if not touch:+ continue+ # Pull detail from the child's own activity tracker+ desc = f"delegate_task: subagent {task_index} working"+ try:+ child_summary = child.get_activity_summary()+ child_tool = child_summary.get("current_tool")+ child_iter = child_summary.get("api_call_count", 0)+ child_max = child_summary.get("max_iterations", 0)+ child_activity_ts = child_summary.get("last_activity_ts")++ # A slow model wait refreshes last_activity_ts (direct_api_call+ # heartbeat), so it never looks stale at the idle threshold.+ iter_advanced = child_iter > _last_seen_iter[0]+ tool_changed = child_tool != _last_seen_tool[0]+ activity_advanced = (+ child_activity_ts is not None+ and (+ _last_seen_activity_ts[0] is None+ or child_activity_ts > _last_seen_activity_ts[0]+ )+ )+ if iter_advanced or tool_changed or activity_advanced:+ _last_seen_iter[0] = child_iter+ _last_seen_tool[0] = child_tool+ if child_activity_ts is not None:+ _last_seen_activity_ts[0] = child_activity_ts+ _stale_count[0] = 0+ else:+ _stale_count[0] += 1++ stale_limit = (+ _HEARTBEAT_STALE_CYCLES_IN_TOOL+ if child_tool+ else _HEARTBEAT_STALE_CYCLES_IDLE+ )+ if _stale_count[0] >= stale_limit:+ logger.warning(+ "Subagent %d appears stale (no progress for %d "+ "heartbeat cycles, tool=%s) — stopping heartbeat",+ task_index,+ _stale_count[0],+ child_tool or "<none>",+ )+ break # stop touching parent, let gateway timeout fire++ if child_tool:+ desc = (+ f"delegate_task: subagent running {child_tool} "+ f"(iteration {child_iter}/{child_max})"+ )+ else:+ child_desc = child_summary.get("last_activity_desc", "")+ if child_desc:+ desc = (+ f"delegate_task: subagent {child_desc} "+ f"(iteration {child_iter}/{child_max})"+ )+ except Exception:+ pass+ try:+ touch(desc)+ except Exception:+ pass++ _heartbeat_thread = threading.Thread(target=_heartbeat_loop, daemon=True)+ return _heartbeat_stop, _heartbeat_thread+++def _register_child(+ child: Any,+ parent_agent: Any,+ goal: str,+ *,+ owner_session_id: Optional[str],+ owner_transport: Any,+ owner_session_record: Any,+) -> Optional[str]:+ """Register the live child in the module registry; return its subagent_id.++ Test doubles without a stable string ``_subagent_id`` are not registered+ (returns None) and the caller skips every registry interaction for them.+ """+ _raw_sid = getattr(child, "_subagent_id", None)+ _subagent_id = _raw_sid if isinstance(_raw_sid, str) else None+ if _subagent_id:+ if owner_session_id is None:+ try:+ from gateway.session_context import get_session_env++ owner_session_id = get_session_env("HERMES_UI_SESSION_ID", "") or None+ except Exception:+ owner_session_id = None+ if owner_session_id and (+ owner_transport is None or owner_session_record is None+ ):+ owner_transport, owner_session_record = (+ _capture_gateway_steer_authority(owner_session_id)+ )+ _raw_depth = getattr(child, "_delegate_depth", 1)+ _tui_depth = max(0, _raw_depth - 1) if isinstance(_raw_depth, int) else 0+ _parent_sid = getattr(child, "_parent_subagent_id", None)+ # Owning conversation's durable session id (same lineage completion+ # delivery routes by); sourced from the child's stamp so it survives a+ # parent_agent rebuild between dispatch and run.+ _owner_agent_session_id = (+ str(getattr(child, "_parent_session_id", "") or "")+ or str(getattr(parent_agent, "session_id", "") or "")+ )+ _delegation_id = getattr(child, "_delegation_id", None)+ _register_subagent(+ {+ "subagent_id": _subagent_id,+ "parent_id": _parent_sid if isinstance(_parent_sid, str) else None,+ "depth": _tui_depth,+ "goal": goal,+ "delegation_id": (+ _delegation_id if isinstance(_delegation_id, str) else None+ ),+ "model": (+ getattr(child, "model", None)+ if isinstance(getattr(child, "model", None), str)+ else None+ ),+ "started_at": time.time(),+ "status": "running",+ "tool_count": 0,+ "agent": child,+ # list/steer/stop ownership when the weakref chain breaks+ # (CLI rebuilds its AIAgent mid-session).+ "owner_agent_session_id": _owner_agent_session_id or None,+ # Immutable live gateway/TUI session that commissioned this+ # child. Empty outside those hosts; RPC authority fails closed.+ "owner_session_id": owner_session_id,+ "owner_transport": owner_transport,+ "owner_session_record": owner_session_record,+ }+ )++ return _subagent_id+++class _WorktreeReporter:+ """Holds the child's worktree-isolation state and reports it into result entries.++ ``info`` stays None until isolation engages, so ``attach`` is a no-op on+ every early error path.+ """++ def __init__(self) -> None:+ self.info: Optional[Dict[str, str]] = None++ def attach(self, entry_dict: Dict[str, Any]) -> None:+ """Inspect + prune the child worktree, reporting into the entry."""+ _worktree_info = self.info+ if _worktree_info is None:+ return+ try:+ from tools import subagent_worktree++ entry_dict["worktree"] = (+ subagent_worktree.finalize_subagent_worktree(_worktree_info)+ )+ except Exception as e:+ # State is unknown: emit the SAME flagged schema the parent expects,+ # via the shared factory so the two producers never drift.+ logger.warning("worktree finalize failed: %s", e)+ try:+ from tools import subagent_worktree as _sw++ entry_dict["worktree"] = _sw.unproven_worktree_payload(+ _worktree_info, f"finalize raised: {e}"+ )+ except Exception:+ # Import itself failed — inline the same shape rather than+ # dropping the flag (the parent must still see the warning).+ entry_dict["worktree"] = {+ "path": _worktree_info.get("path", ""),+ "branch": _worktree_info.get("branch", ""),+ "commits": 0,+ "dirty": False,+ "pruned": False,+ "inspection_failed": True,+ "note": (+ f"worktree finalize raised ({e}) and the reporting "+ "helper was unavailable: 'commits' and 'dirty' are "+ "UNKNOWN, not zero/clean. Inspect "+ f"{_worktree_info.get('path', '')} before assuming "+ "no work."+ ),+ }+++@dataclass+class _ChildWorkspace:+ child_task_id: str+ parent_task_id: Optional[str]+ goal: str+ wall_start: float+ parent_reads_snapshot: list+++def _seed_child_workspace(+ child: Any,+ parent_agent: Any,+ goal: str,+ task_index: int,+ subagent_id: Optional[str],+ worktree: _WorktreeReporter,+) -> _ChildWorkspace:+ """Seed cwd/container aliases and optional worktree isolation for the child.++ Returns the ids the run needs; ``goal`` comes back extended with the+ worktree contract note when isolation engaged.+ """+ from tools.delegate_tool import _get_worktree_isolation, _resolve_workspace_hint++ _subagent_id = subagent_id+ _worktree_info = None+ import uuid as _uuid++ child_task_id = _subagent_id or f"subagent-{task_index}-{_uuid.uuid4().hex[:8]}"+ parent_task_id = getattr(parent_agent, "_current_task_id", None)+ # Seed the child's cwd record from the parent's: same starting directory,+ # but the child's later `cd`s stay in its own record.+ try:+ from tools.terminal_tool import (+ get_session_cwd,+ record_session_cwd,+ register_container_alias,+ )++ record_session_cwd(child_task_id, get_session_cwd(parent_task_id))+ # Per-session container isolation keys containers by task_id; the+ # child must share the PARENT's container.+ register_container_alias(child_task_id, parent_task_id)+ except Exception as e:+ logger.debug("Child cwd seed failed: %s", e)++ # Opt-in worktree isolation: own git worktree off the parent's HEAD, terminal+ # started there. Git-only, local-backend-only; failures degrade silently.+ if _get_worktree_isolation():+ try:+ from tools import subagent_worktree++ if subagent_worktree.local_backend_active():+ _parent_cwd = None+ try:+ from tools.terminal_tool import get_session_cwd as _gsc++ _parent_cwd = _gsc(parent_task_id)+ except Exception:+ pass+ _worktree_info = subagent_worktree.create_subagent_worktree(+ _parent_cwd or _resolve_workspace_hint(parent_agent),+ subagent_id=_subagent_id,+ )+ else:+ logger.debug(+ "worktree isolation skipped: non-local terminal backend"+ )+ except Exception as e:+ logger.debug("worktree isolation setup failed: %s", e)+ if _worktree_info is not None:+ try:+ from tools.terminal_tool import record_session_cwd as _rsc++ _rsc(child_task_id, _worktree_info["path"])+ except Exception as e:+ logger.debug("worktree cwd seed failed: %s", e)+ # The child's context is already built; carry the isolation+ # contract on the goal message instead (same turn, no+ # system-prompt mutation).+ from tools.subagent_worktree import build_worktree_context_note++ goal = goal + build_worktree_context_note(_worktree_info)++ wall_start = time.time()+ parent_reads_snapshot = (+ list(file_state.known_reads(parent_task_id)) if parent_task_id else []+ )+ worktree.info = _worktree_info+ return _ChildWorkspace(child_task_id, parent_task_id, goal, wall_start, parent_reads_snapshot)+++@dataclass+class _ChildFailure:+ entry: Dict[str, Any]+ close_deferred: bool+++def _handle_child_wait_failure(+ exc: BaseException,+ *,+ child: Any,+ task_index: int,+ goal: str,+ subagent_id: Optional[str],+ child_future: Any,+ child_timeout: Optional[float],+ child_start: float,+ child_progress_cb: Any,+ worker_thread_holder: Dict[str, Optional[threading.Thread]],+ worktree: _WorktreeReporter,+) -> _ChildFailure:+ """Build the error entry for a child whose Future timed out or raised.++ Signals the child to stop, dumps the 0-API-call diagnostic, and on a+ timeout hands ``child.close()`` to a Future done-callback (returned as+ ``close_deferred=True``) because closing from this thread races the+ still-unwinding worker's finally path.+ """+ _timeout_exc = exc+ _subagent_id = subagent_id+ _child_future = child_future+ _worker_thread_holder = worker_thread_holder+ _attach_worktree = worktree.attach+ _child_close_deferred = False+ # No consumer boundary remains once this owner stops waiting for+ # the child. Close acceptance before any completion callback and+ # retain steer text that won the race with this failure/timeout.+ _late_pending_steer = (+ _close_subagent_steering(_subagent_id, child) if _subagent_id else None+ )+ # Signal the child to stop so its thread can exit cleanly.+ try:+ interrupted = child is not None and request_hard_interrupt(child)+ if not interrupted and child is not None and hasattr(child, "_interrupt_requested"):+ child._interrupt_requested = True+ except Exception:+ pass++ is_timeout = isinstance(_timeout_exc, (FuturesTimeoutError, TimeoutError))+ duration = round(time.monotonic() - child_start, 2)+ logger.warning(+ "Subagent %d %s after %.1fs",+ task_index,+ "timed out" if is_timeout else f"raised {type(_timeout_exc).__name__}",+ duration,+ )++ # When a subagent times out BEFORE making any API call, dump a+ # diagnostic to help users (and us) see what the child was doing.+ # See #14726 — without this, 0-API-call hangs are black boxes.+ diagnostic_path: Optional[str] = None+ child_api_calls = 0+ try:+ _summary = child.get_activity_summary()+ child_api_calls = int(_summary.get("api_call_count", 0) or 0)+ except Exception:+ pass+ if is_timeout and child_api_calls == 0:+ diagnostic_path = _dump_subagent_timeout_diagnostic(+ child=child,+ task_index=task_index,+ # is_timeout implies a cap was configured (result(timeout=None)+ # never raises FuturesTimeoutError); guard for the type checker.+ timeout_seconds=float(child_timeout or 0.0),+ duration_seconds=float(duration),+ worker_thread=_worker_thread_holder.get("t"),+ goal=goal,+ )+ if diagnostic_path:+ logger.warning(+ "Subagent %d 0-API-call timeout — diagnostic written to %s",+ task_index,+ diagnostic_path,+ )++ if child_progress_cb:+ try:+ child_progress_cb(+ "subagent.complete",+ preview=(+ f"Timed out after {duration}s"+ if is_timeout+ else str(_timeout_exc)+ ),+ status="timeout" if is_timeout else "error",+ duration_seconds=duration,+ summary="",+ )+ except Exception:+ pass++ if is_timeout:+ if child_api_calls == 0:+ _err = (+ f"Subagent timed out after {child_timeout}s without "+ f"making any API call — the child never reached its "+ f"first LLM request (prompt construction, credential "+ f"resolution, or transport may be stuck)."+ )+ if diagnostic_path:+ _err += f" Diagnostic: {diagnostic_path}"+ else:+ _err = (+ f"Subagent timed out after {child_timeout}s with "+ f"{child_api_calls} API call(s) completed — likely "+ f"stuck on a slow API call, tool call, or unresponsive "+ f"network request."+ )+ if diagnostic_path:+ _err += f" Diagnostic: {diagnostic_path}"+ else:+ _err = str(_timeout_exc)++ _error_entry = {+ "task_index": task_index,+ "status": "timeout" if is_timeout else "error",+ "summary": None,+ "error": _err,+ "exit_reason": "timeout" if is_timeout else "error",+ "api_calls": child_api_calls,+ "duration_seconds": duration,+ "timeout_seconds": child_timeout if is_timeout else None,+ "timed_out_after_seconds": duration if is_timeout else None,+ "timeout_phase": (+ "before_first_llm_call" if is_timeout and child_api_calls == 0+ else "after_llm_calls" if is_timeout+ else None+ ),+ "_child_role": getattr(child, "_delegate_role", None),+ "diagnostic_path": diagnostic_path,+ }+ if _late_pending_steer:+ _error_entry["missed_steer"] = _late_pending_steer+ _error_entry["error"] += (+ " [steer did not land before the subagent stopped: "+ f"{_late_pending_steer}]"+ )+ _attach_worktree(_error_entry)+ if is_timeout and not _child_future.done():+ # The interrupt is cooperative: the worker still runs its finally path,+ # so closing the child now could close SQLite under its final write.+ # A Future done-callback is the first safe close boundary.+ def _close_after_timed_out_worker(_done_future) -> None:+ try:+ close = getattr(child, "close", None)+ if callable(close):+ close()+ except Exception:+ logger.debug(+ "Failed to close timed-out child after worker exit",+ exc_info=True,+ )++ _child_future.add_done_callback(_close_after_timed_out_worker)+ _child_close_deferred = True++ # The abandoned worker is usually parked in an OpenSSL read. NEVER+ # hard-close that transport from this thread (cross-thread FD release+ # under a live SSL read corrupts native state); shutdown() the pooled+ # sockets instead — FD-safe, settles the read with EOF so the worker+ # unwinds. One immediate sweep + one delayed re-sweep for a connection+ # opened in between; a worker that still won't settle keeps its+ # resources until process exit.+ _drain = getattr(child, "_drain_transports_after_abandonment", None)+ if callable(_drain):+ def _drain_once(phase: str) -> None:+ try:+ _drain(reason=f"delegate_timeout_{phase}")+ except Exception:+ logger.debug(+ "Timed-out child transport drain (%s) failed",+ phase,+ exc_info=True,+ )++ _drain_once("immediate")++ def _drain_resweep() -> None:+ if not _child_future.done():+ _drain_once("resweep")++ _resweep_timer = threading.Timer(5.0, _drain_resweep)+ _resweep_timer.daemon = True+ _resweep_timer.start()+ return _ChildFailure(_error_entry, _child_close_deferred)+++@dataclass+class _SchemaOutcome:+ schema: Optional[Dict[str, Any]]+ valid: Optional[bool]+ errors: List[str]+ retries: int+++def _validate_child_output_schema(+ child: Any, result: Dict[str, Any], task_index: int, child_task_id: str, relay_child_text: Any+) -> _SchemaOutcome:+ """Validate the final answer against the attached output_schema with ONE bounded retry.++ Schema-less children (no dict on ``child._delegate_output_schema``) take no+ branch here so their result entry stays byte-identical.+ """+ _relay_child_text = relay_child_text+ _output_schema = getattr(child, "_delegate_output_schema", None)+ _schema_valid: Optional[bool] = None+ _schema_errors: List[str] = []+ _schema_retries = 0+ if isinstance(_output_schema, dict):+ from tools.delegation_output_schema import (+ build_retry_message,+ validate_output,+ )++ _first_text = result.get("final_response") or ""+ _schema_valid, _schema_errors = validate_output(+ _first_text, _output_schema+ )+ if (+ not _schema_valid+ and _first_text.strip()+ and not result.get("interrupted", False)+ ):+ # Exactly one retry turn, carrying the validation errors+ # verbatim (no schema re-paste — the child already holds+ # the contract in its context).+ _schema_retries = 1+ _retry_result = None+ try:+ _retry_result = child.run_conversation(+ user_message=build_retry_message(_schema_errors),+ task_id=child_task_id,+ stream_callback=_relay_child_text,+ )+ except Exception as _retry_exc:+ logger.warning(+ "Subagent %d schema-retry turn failed: %s",+ task_index,+ _retry_exc,+ )+ if isinstance(_retry_result, dict):+ _retry_text = _retry_result.get("final_response") or ""+ if _retry_text.strip():+ result["final_response"] = _retry_text+ try:+ result["api_calls"] = int(+ result.get("api_calls", 0) or 0+ ) + int(_retry_result.get("api_calls", 0) or 0)+ except (TypeError, ValueError):+ pass+ _retry_messages = _retry_result.get("messages")+ if isinstance(_retry_messages, list) and isinstance(+ result.get("messages"), list+ ):+ result["messages"] = result["messages"] + _retry_messages+ _schema_valid, _schema_errors = validate_output(+ _retry_text, _output_schema+ )++ # Linearization boundary for registry steering. From this point on the+ # child cannot consume another steer. Closing under the registry lock+ # either rejects a concurrent caller or drains every previously accepted+ return _SchemaOutcome(_output_schema, _schema_valid, _schema_errors, _schema_retries)+++def _build_result_entry(+ child: Any,+ result: Dict[str, Any],+ task_index: int,+ duration: float,+ schema: _SchemaOutcome,+) -> Dict[str, Any]:+ """Derive the parent-visible result entry (status, exit_reason, tool trace, tokens, cost).++ ``status`` / ``exit_reason`` / ``truncated`` follow the contract in the+ ``_run_single_child`` docstring; a structured failure always wins over the+ summary-presence heuristic (which is only a fallback for legacy/mock results).+ """+ _output_schema, _schema_valid, _schema_errors, _schema_retries = (+ schema.schema, schema.valid, schema.errors, schema.retries+ )+ summary = result.get("final_response") or ""+ completed = result.get("completed", False)+ interrupted = result.get("interrupted", False)+ api_calls = result.get("api_calls", 0)++ # "(empty)" is run_agent's give-up sentinel after repeated empty LLM+ # responses (usually a transport bug) — a failure, not a success.+ _empty_sentinel = summary.strip() == "(empty)"++ if interrupted:+ status = "interrupted"+ elif result.get("failed") or result.get("error"):+ # A structured failure WINS over the summary heuristic: the loop returns+ # the error text as final_response, which would otherwise read as+ # "completed". The heuristic only covers legacy/mock results.+ status = "failed"+ elif _schema_valid is False:+ # Declared schema still violated after the bounded retry: the summary+ # is unusable under the contract, so status must not say completed+ # (orchestrators reading only status/icon would accept an empty+ # verdict). None on schema-less runs, which never take this branch.+ status = "failed"+ elif summary and not _empty_sentinel:+ # A summary means the subagent produced usable output.+ # exit_reason ("completed" vs "max_iterations") already+ # tells the parent *how* the task ended.+ status = "completed"+ else:+ status = "failed"++ # Build tool trace from conversation messages (already in memory).+ # Uses tool_call_id to correctly pair parallel tool calls with results.+ tool_trace: list[Dict[str, Any]] = []+ trace_by_id: Dict[str, Dict[str, Any]] = {}+ messages = result.get("messages") or []+ if isinstance(messages, list):+ for msg in messages:+ if not isinstance(msg, dict):+ continue+ if msg.get("role") == "assistant":+ for tc in msg.get("tool_calls") or []:+ fn = tc.get("function", {})+ arguments = fn.get("arguments", "")+ entry_t = {+ "tool": fn.get("name", "unknown"),+ "args_bytes": len(arguments),+ "input_summary": _summarize_tool_arguments(arguments),+ }+ tool_trace.append(entry_t)+ tc_id = tc.get("id")+ if tc_id:+ trace_by_id[tc_id] = entry_t+ elif msg.get("role") == "tool":+ content = _stringify_tool_content(msg.get("content", ""))+ is_error = _looks_like_error_output(content)+ result_meta = {+ "result_bytes": len(content),+ "status": "error" if is_error else "ok",+ }+ # Match by tool_call_id for parallel calls+ tc_id = msg.get("tool_call_id")+ target = trace_by_id.get(tc_id) if tc_id else None+ if target is not None:+ target.update(result_meta)+ elif tool_trace:+ # Fallback for messages without tool_call_id+ tool_trace[-1].update(result_meta)++ # Determine exit reason+ if interrupted:+ exit_reason = "interrupted"+ elif result.get("failed") or result.get("error"):+ # Provider rejection / terminal failure. Do NOT report this as+ # iteration-budget exhaustion — "max_iterations" is only truthful+ # when the child actually hit its per-delegation iteration cap.+ exit_reason = "error"+ elif completed:+ exit_reason = "completed"+ else:+ # Genuine budget exhaustion: completed=False with no failure.+ exit_reason = "max_iterations"++ # Extract token counts (safe for mock objects)+ _input_tokens = getattr(child, "session_prompt_tokens", 0)+ _output_tokens = getattr(child, "session_completion_tokens", 0)+ _model = getattr(child, "model", None)++ # Result entry contract: see the _run_single_child docstring.+ entry: Dict[str, Any] = {+ "task_index": task_index,+ "status": status,+ "summary": summary,+ "api_calls": api_calls,+ "duration_seconds": duration,+ "model": _model if isinstance(_model, str) else None,+ "exit_reason": exit_reason,+ # A budget-exhausted child still returns a summary (status stays+ # "completed"), so the parent needs this explicit flag.+ "truncated": exit_reason == "max_iterations",+ "tokens": {+ "input": (+ _input_tokens if isinstance(_input_tokens, (int, float)) else 0+ ),+ "output": (+ _output_tokens if isinstance(_output_tokens, (int, float)) else 0+ ),+ },+ "tool_trace": tool_trace,+ # Captured before the finally block calls child.close() so the+ # parent thread can fire subagent_stop with the correct role.+ # Stripped before the dict is serialised back to the model.+ "_child_role": getattr(child, "_delegate_role", None),+ # Folded into the parent's session cost by the aggregator; stripped+ # before serialisation back to the model.+ "_child_cost_usd": (+ float(getattr(child, "session_estimated_cost_usd", 0.0) or 0.0)+ if isinstance(+ getattr(child, "session_estimated_cost_usd", 0.0),+ (int, float),+ )+ else 0.0+ ),+ }+ # Model-visible per-delegation spend (unlike _child_cost_usd above).+ entry["cost_usd"] = round(entry["_child_cost_usd"], 6)+ _cost_status = getattr(child, "session_cost_status", None)+ entry["cost_status"] = (+ _cost_status if isinstance(_cost_status, str) and _cost_status+ else "unknown"+ )+ if status == "failed":+ if _schema_valid is False and summary and not _empty_sentinel:+ # The child DID respond; name the contract violation instead of+ # the generic "no response" error.+ entry["error"] = (+ "Final answer does not satisfy the declared "+ "output_schema (after 1 retry)."+ if _schema_retries+ else "Final answer does not satisfy the declared "+ "output_schema."+ )+ else:+ entry["error"] = result.get(+ "error", "Subagent did not produce a response."+ )+ # Classified reason from the child loop (e.g. "rate_limit",+ # "billing", "server_error") — lets the parent distinguish a+ # quota wall from a real task error without parsing prose.+ _failure_reason = result.get("failure_reason")+ if isinstance(_failure_reason, str) and _failure_reason:+ entry["failure_reason"] = _failure_reason++ # T1-24: schema-validation outcome — emitted ONLY when a schema was+ # requested, so legacy (schema-less) payloads keep their exact shape.+ if isinstance(_output_schema, dict):+ entry["schema_valid"] = bool(_schema_valid)+ if _schema_retries:+ entry["schema_retries"] = _schema_retries+ if not _schema_valid and _schema_errors:+ entry["schema_errors"] = _schema_errors++ # A steer queued after the final assistant turn had no tool batch to land+ # in; the finalizer hands it back as "pending_steer". Name it so the parent+ # sees it was MISSED rather than silently absorbed.+ _missed_steer = result.get("pending_steer")+ if isinstance(_missed_steer, str) and _missed_steer.strip():+ entry["missed_steer"] = _missed_steer+ _miss_note = (+ "[steer did not land — the subagent finished before it could "+ f"be delivered: {_missed_steer}]"+ )+ entry["summary"] = f"{summary}\n\n{_miss_note}" if summary else _miss_note+ return entry+++def _append_sibling_write_reminder(entry: Dict[str, Any], ws: _ChildWorkspace) -> None:+ """Warn the parent when this child wrote files the parent had already read.++ Checks writes by ANY non-parent task_id (not just this child's) so nested+ orchestrator→worker chains are covered too.+ """+ parent_task_id, wall_start, parent_reads_snapshot = (+ ws.parent_task_id, ws.wall_start, ws.parent_reads_snapshot+ )+ try:+ if parent_task_id and parent_reads_snapshot:+ sibling_writes = file_state.writes_since(+ parent_task_id, wall_start, parent_reads_snapshot+ )+ if sibling_writes:+ mod_paths = sorted(+ {p for paths in sibling_writes.values() for p in paths}+ )+ if mod_paths:+ reminder = (+ "\n\n[NOTE: subagent modified files the parent "+ "previously read — re-read before editing: "+ + ", ".join(mod_paths[:8])+ + (+ f" (+{len(mod_paths) - 8} more)"+ if len(mod_paths) > 8+ else ""+ )+ + "]"+ )+ if entry.get("summary"):+ entry["summary"] = entry["summary"] + reminder+ else:+ entry["stale_paths"] = mod_paths+ except Exception:+ logger.debug("file_state sibling-write check failed", exc_info=True)+++def _emit_child_complete(+ child: Any,+ result: Dict[str, Any],+ entry: Dict[str, Any],+ ws: _ChildWorkspace,+ duration: float,+ child_progress_cb: Any,+) -> None:+ """Fire ``subagent.complete`` with the per-branch observability payload.++ Tokens, cost, files touched and a tool-output tail feed the TUI overlay;+ every field is optional and degrades gracefully on the client.+ """+ if not child_progress_cb:+ return+ summary, status, api_calls = entry["summary"], entry["status"], entry["api_calls"]+ _input_tokens = getattr(child, "session_prompt_tokens", 0)+ _output_tokens = getattr(child, "session_completion_tokens", 0)+ child_task_id, wall_start = ws.child_task_id, ws.wall_start+ _cost_usd = getattr(child, "session_estimated_cost_usd", None)+ _reasoning_tokens = getattr(child, "session_reasoning_tokens", 0)+ try:+ _files_read = list(file_state.known_reads(child_task_id))[:40]+ except Exception:+ _files_read = []+ try:+ _files_written_map = file_state.writes_since(+ "", wall_start, []+ ) # all writes since wall_start+ except Exception:+ _files_written_map = {}+ _files_written = sorted(+ {+ p+ for tid, paths in _files_written_map.items()+ if tid == child_task_id+ for p in paths+ }+ )[:40]++ _output_tail = _extract_output_tail(result, max_entries=8, max_chars=600)++ complete_kwargs: Dict[str, Any] = {+ "preview": summary[:160] if summary else entry.get("error", ""),+ "status": status,+ "duration_seconds": duration,+ "summary": summary[:500] if summary else entry.get("error", ""),+ "input_tokens": (+ int(_input_tokens) if isinstance(_input_tokens, (int, float)) else 0+ ),+ "output_tokens": (+ int(_output_tokens) if isinstance(_output_tokens, (int, float)) else 0+ ),+ "reasoning_tokens": (+ int(_reasoning_tokens)+ if isinstance(_reasoning_tokens, (int, float))+ else 0+ ),+ "api_calls": int(api_calls) if isinstance(api_calls, (int, float)) else 0,+ "files_read": _files_read,+ "files_written": _files_written,+ "output_tail": _output_tail,+ }+ if _cost_usd is not None:+ try:+ complete_kwargs["cost_usd"] = float(_cost_usd)+ except (TypeError, ValueError):+ pass++ if child_progress_cb:+ try:+ child_progress_cb("subagent.complete", **complete_kwargs)+ except Exception as e:+ logger.debug("Progress callback completion failed: %s", e)+++def _cleanup_child_run(+ child: Any,+ parent_agent: Any,+ *,+ subagent_id: Optional[str],+ heartbeat: tuple,+ child_pool: Any,+ leased_cred_id: Any,+ close_deferred: bool,+) -> None:+ """Finally-path teardown for one child run (idempotent, never raises).++ Order matters: stop heartbeat → drop registry entry → release credential+ lease → restore the parent's process-global tool names → detach from the+ parent's interrupt list → close the child (unless a timed-out worker still+ owns it) → pop the child's Relay scope if no turn is active.+ """+ _heartbeat_stop, _heartbeat_thread = heartbeat+ _subagent_id = subagent_id+ _child_close_deferred = close_deferred+ _heartbeat_stop.set()+ if _heartbeat_thread.ident is not None:+ _heartbeat_thread.join(timeout=5)++ # Drop the TUI-facing registry entry. Safe to call even if the+ # child was never registered (e.g. ID missing on test doubles).+ if _subagent_id:+ _unregister_subagent(_subagent_id, agent=child)++ if child_pool is not None and leased_cred_id is not None:+ try:+ child_pool.release_lease(leased_cred_id)+ except Exception as exc:+ logger.debug("Failed to release credential lease: %s", exc)++ # Restore the parent's tool names so the process-global is correct+ # for any subsequent execute_code calls or other consumers.+ import model_tools++ saved_tool_names = getattr(child, "_delegate_saved_tool_names", None)+ if isinstance(saved_tool_names, list):+ model_tools._last_resolved_tool_names = list(saved_tool_names)++ # Remove child from active tracking++ # Unregister child from interrupt propagation+ if hasattr(parent_agent, "_active_children"):+ try:+ lock = getattr(parent_agent, "_active_children_lock", None)+ if lock:+ with lock:+ parent_agent._active_children.remove(child)+ else:+ parent_agent._active_children.remove(child)+ except (ValueError, UnboundLocalError) as e:+ logger.debug("Could not remove child from active_children: %s", e)++ # Close tool resources (terminal sandboxes, browser daemons,+ # background processes, httpx clients) so subagent subprocesses+ # don't outlive the delegation.+ if not _child_close_deferred:+ try:+ close = getattr(child, "close", None)+ if callable(close):+ close()+ except Exception:+ logger.debug("Failed to close child agent after delegation")++ # The AIAgent turn boundary normally closes the child scope itself. This+ # fallback covers failures before that boundary starts, but must not pop+ # a scope while a timed-out child worker is still unwinding.+ try:+ from agent import relay_runtime++ runtime = relay_runtime.get_runtime(create=False)+ child_session_id = str(getattr(child, "session_id", "") or "")+ child_turn_is_active = relay_runtime.SESSION_COORDINATOR.has_active_turn(+ profile_key=relay_runtime.current_profile_key(),+ session_id=child_session_id,+ )+ if runtime is not None and child_session_id and not child_turn_is_active:+ runtime.unregister_subagent({"child_session_id": child_session_id})+ except Exception:+ logger.debug("Failed to close child Relay session after delegation")+diff --git a/tools/delegate_tool_config.py b/tools/delegate_tool_config.pynew file mode 100644index 00000000000..8a82fff102c--- /dev/null+++ b/tools/delegate_tool_config.py@@ -0,0 +1,560 @@+"""Delegation config knobs (delegation.* keys) and child credential/provider resolution.++Split out of ``tools/delegate_tool.py``; every moved name is re-imported there, so+``tools.delegate_tool.<name>`` keeps resolving (and monkeypatching) as before.+"""++from __future__ import annotations++import logging+import os+from typing import Any, Optional+from utils import base_url_hostname, is_truthy_value++# Log-record parity with the origin module.+logger = logging.getLogger("tools.delegate_tool")++# Runtime-provider sentinel for providers that are not natively known; must+# match hermes_cli.runtime_provider.RUNTIME_PROVIDER_TYPE_CUSTOM.+_RUNTIME_PROVIDER_CUSTOM = "custom"++_DEFAULT_MAX_CONCURRENT_CHILDREN = 10+# One-shot guard: _get_max_concurrent_children() runs on every get_definitions()+# schema rebuild, so the >10 cost advisory would otherwise log on every turn.+_HIGH_CONCURRENCY_WARNED = False+MAX_DEPTH = 1 # flat by default: parent (0) -> child (1); deeper needs max_spawn_depth+_MIN_SPAWN_DEPTH = 1 # floor for the configurable cap; MAX_DEPTH stays the default+_LEGACY_MAX_ASYNC_WARNED = False+# No default wall-clock cap on children: legitimate heavy work (deep reviews,+# research fan-outs, slow reasoning models) was being killed mid-task. Stuck-child+# detection is the heartbeat staleness monitor; delegation.child_timeout_seconds+# opts back in.+DEFAULT_CHILD_TIMEOUT: Optional[float] = None+++# ── Subagent approval callbacks ─────────────────────────────────────────────+# Subagent worker threads don't inherit the CLI's threading.local approval+# callback, so prompt_dangerous_approval() would fall back to input() and+# deadlock the parent's prompt_toolkit TUI. Every worker gets a non-interactive+# callback via ThreadPoolExecutor(initializer=...): deny by default, approve when+# delegation.subagent_auto_approve is true. Both warn for audit. Gateway sessions+# are unaffected (they resolve approvals via tools/approval.py's per-session queue).+def _subagent_auto_deny(command: str, description: str, **kwargs) -> str:+ """Auto-deny (safe default): returns 'deny' so the child sees a recoverable refusal."""+ logger.warning(+ "Subagent auto-denied dangerous command: %s (%s). "+ "Set delegation.subagent_auto_approve: true to allow.",+ command, description,+ )+ return "deny"+++def _subagent_auto_approve(command: str, description: str, **kwargs) -> str:+ """Auto-approve (opt-in YOLO via delegation.subagent_auto_approve): returns 'once'."""+ logger.warning(+ "Subagent auto-approved dangerous command: %s (%s)",+ command, description,+ )+ return "once"+++def _get_subagent_approval_callback():+ """Callback for subagent worker threads per delegation.subagent_auto_approve (default False)."""+ from tools.delegate_tool import _load_config+ cfg = _load_config()+ val = cfg.get("subagent_auto_approve", False)+ if is_truthy_value(val):+ return _subagent_auto_approve+ return _subagent_auto_deny+++def _get_max_concurrent_children() -> int:+ """delegation.max_concurrent_children > DELEGATION_MAX_CONCURRENT_CHILDREN env > 10.++ Floor of 1 is the only bound enforced; there is no ceiling.+ """+ from tools.delegate_tool import _load_config+ cfg = _load_config()+ val = cfg.get("max_concurrent_children")+ if val is not None:+ try:+ result = max(1, int(val))+ if result > 10:+ global _HIGH_CONCURRENCY_WARNED+ if not _HIGH_CONCURRENCY_WARNED:+ _HIGH_CONCURRENCY_WARNED = True+ logger.warning(+ "delegation.max_concurrent_children=%d: each child consumes API tokens "+ "independently. High values multiply cost linearly.",+ result,+ )+ return result+ except (TypeError, ValueError):+ logger.warning(+ "delegation.max_concurrent_children=%r is not a valid integer; "+ "using default %d",+ val,+ _DEFAULT_MAX_CONCURRENT_CHILDREN,+ )+ return _DEFAULT_MAX_CONCURRENT_CHILDREN+ env_val = os.getenv("DELEGATION_MAX_CONCURRENT_CHILDREN")+ if env_val:+ try:+ return max(1, int(env_val))+ except (TypeError, ValueError):+ return _DEFAULT_MAX_CONCURRENT_CHILDREN+ return _DEFAULT_MAX_CONCURRENT_CHILDREN+++def _get_worktree_isolation() -> bool:+ """delegation.worktree_isolation (bool, default False).++ When enabled each child gets its own git worktree off the parent's HEAD so+ parallel children never contend for one working copy. Git-only and+ local-backend-only; otherwise silently ignored (shared workspace as before).+ """+ from tools.delegate_tool import _load_config+ cfg = _load_config()+ return bool(cfg.get("worktree_isolation", False))+++def _get_max_async_children() -> int:+ """Concurrency cap for background delegations == delegation.max_concurrent_children.++ At capacity a new async dispatch is REJECTED (not queued) so a runaway model+ can't pile up unbounded background work; the caller then runs synchronously.+ A leftover ``delegation.max_async_children`` key is ignored with a one-time+ deprecation warning.+ """+ from tools.delegate_tool import _get_max_concurrent_children, _load_config+ global _LEGACY_MAX_ASYNC_WARNED+ cfg = _load_config()+ if cfg.get("max_async_children") is not None and not _LEGACY_MAX_ASYNC_WARNED:+ _LEGACY_MAX_ASYNC_WARNED = True+ logger.warning(+ "delegation.max_async_children is deprecated and ignored; "+ "delegation.max_concurrent_children now caps background "+ "delegations too. Remove the stale key from config.yaml."+ )+ return _get_max_concurrent_children()+++def _get_child_timeout() -> Optional[float]:+ """Hard wall-clock cap for one child, or None (default: no timeout).++ Failures should come from what the child does (API/tool errors, iteration+ budget), not a stopwatch; stuck children are caught by the heartbeat+ staleness monitor. delegation.child_timeout_seconds > 0 opts in (floor 30 s);+ 0 or negative disables. Env fallback: DELEGATION_CHILD_TIMEOUT_SECONDS.+ """+ from tools.delegate_tool import _load_config+ cfg = _load_config()+ val = cfg.get("child_timeout_seconds")+ if val is not None:+ try:+ parsed = float(val)+ except (TypeError, ValueError):+ logger.warning(+ "delegation.child_timeout_seconds=%r is not a valid number; "+ "using default (no timeout)",+ val,+ )+ else:+ return None if parsed <= 0 else max(30.0, parsed)+ env_val = os.getenv("DELEGATION_CHILD_TIMEOUT_SECONDS")+ if env_val:+ try:+ parsed = float(env_val)+ except (TypeError, ValueError):+ pass+ else:+ return None if parsed <= 0 else max(30.0, parsed)+ return DEFAULT_CHILD_TIMEOUT+++def _get_max_spawn_depth() -> int:+ """delegation.max_spawn_depth floored at 1 (no ceiling).++ Depth 0 is the parent; agents at depths 0..N-1 may spawn, depth N is the+ leaf floor. Default 1 is flat. Each extra level multiplies API cost.+ """+ from tools.delegate_tool import _load_config+ cfg = _load_config()+ val = cfg.get("max_spawn_depth")+ if val is None:+ return MAX_DEPTH+ try:+ ival = int(val)+ except (TypeError, ValueError):+ logger.warning(+ "delegation.max_spawn_depth=%r is not a valid integer; " "using default %d",+ val,+ MAX_DEPTH,+ )+ return MAX_DEPTH+ floored = max(_MIN_SPAWN_DEPTH, ival)+ if floored != ival:+ logger.warning(+ "delegation.max_spawn_depth=%d below floor %d; using %d",+ ival,+ _MIN_SPAWN_DEPTH,+ floored,+ )+ return floored+++def _get_orchestrator_enabled() -> bool:+ """delegation.orchestrator_enabled kill switch (default True): False forces every child to leaf."""+ from tools.delegate_tool import _load_config+ cfg = _load_config()+ val = cfg.get("orchestrator_enabled", True)+ if isinstance(val, bool):+ return val+ # Accept "true"/"false" strings from YAML that doesn't auto-coerce.+ if isinstance(val, str):+ return val.strip().lower() in {"true", "1", "yes", "on"}+ return True+++def _get_inherit_mcp_toolsets() -> bool:+ """Whether narrowed child toolsets should keep the parent's MCP toolsets."""+ from tools.delegate_tool import _load_config+ cfg = _load_config()+ return is_truthy_value(cfg.get("inherit_mcp_toolsets"), default=True)+++def _normalized_runtime_url(value: Any) -> str:+ return str(value or "").strip().rstrip("/")++def _inherit_parent_capabilities(+ parent_agent, override_provider, override_base_url+) -> Optional[dict]:+ """Parent's endpoint-trust capability map for a child, or None.++ ``agent.capabilities`` is a trust decision scoped to one provider+endpoint:+ inherited ONLY when the child runs the parent's exact route; any provider or+ base_url override stays DEFAULT-DENY (matches the /model switch posture).+ """+ if override_provider or override_base_url:+ return None+ parent_caps = getattr(parent_agent, "capabilities", None)+ if not isinstance(parent_caps, dict):+ return None+ return {+ key: value+ for key, value in parent_caps.items()+ if isinstance(key, str) and isinstance(value, bool)+ }+++def _inherit_parent_base_url(parent_agent, fallback_base_url: Optional[str]) -> Optional[str]:+ """Base URL the parent is actually calling (live client), not a stale attribute.++ ``parent_agent.base_url`` can lag the live client (old OpenRouter URL vs+ local Ollama); inheriting the stale one 401s with a dummy/local key.+ """+ surface_url = _normalized_runtime_url(fallback_base_url)+ client_kwargs = getattr(parent_agent, "_client_kwargs", None)+ if isinstance(client_kwargs, dict):+ kwargs_url = _normalized_runtime_url(client_kwargs.get("base_url"))+ if (+ kwargs_url+ and kwargs_url != surface_url+ and kwargs_url.startswith(("http://", "https://"))+ ):+ return kwargs_url++ client = getattr(parent_agent, "client", None)+ if client is not None:+ # OpenAI SDK exposes base_url as httpx.URL — coerce before comparing.+ live_url = _normalized_runtime_url(getattr(client, "base_url", ""))+ if (+ live_url+ and live_url != surface_url+ and live_url.startswith(("http://", "https://"))+ ):+ return live_url++ return fallback_base_url or None+++def _resolve_child_credential_pool(+ effective_provider: Optional[str],+ parent_agent,+ effective_base_url: Optional[str] = None,+):+ """Credential pool for the child: parent's pool (same provider), that+ provider's own pool, or None (child keeps its fixed credential).++ Custom endpoints all collapse to ``provider="custom"``, so they are matched+ by endpoint identity (the ``custom:<name>`` pool key) — sharing the parent's+ pool across different custom endpoints would overwrite the child's delegated+ base_url on lease.+ """+ if not effective_provider:+ return getattr(parent_agent, "_credential_pool", None)++ parent_provider = getattr(parent_agent, "provider", None) or ""+ parent_pool = getattr(parent_agent, "_credential_pool", None)++ if effective_provider == "custom":+ try:+ from agent.credential_pool import get_custom_provider_pool_key, load_pool++ child_key = get_custom_provider_pool_key(effective_base_url)+ if child_key is None:+ # Unregistered endpoint (no custom_providers entry): keep the+ # child's fixed credential rather than inherit the parent's.+ return None+ parent_key = get_custom_provider_pool_key(+ getattr(parent_agent, "base_url", None)+ )+ if (+ parent_pool is not None+ and parent_provider == "custom"+ and parent_key is not None+ and parent_key == child_key+ ):+ return parent_pool++ pool = load_pool(child_key)+ if pool is not None and pool.has_credentials():+ return pool+ except Exception as exc:+ logger.debug(+ "Could not resolve custom credential pool for child endpoint '%s': %s",+ effective_base_url,+ exc,+ )+ return None++ if parent_pool is not None and effective_provider == parent_provider:+ return parent_pool++ try:+ from agent.credential_pool import load_pool++ pool = load_pool(effective_provider)+ if pool is not None and pool.has_credentials():+ return pool+ except Exception as exc:+ logger.debug(+ "Could not load credential pool for child provider '%s': %s",+ effective_provider,+ exc,+ )+ return None+++def _merge_request_overrides(runtime_overrides, explicit_overrides):+ """Merge explicit ``delegation.request_overrides`` OVER runtime-derived ones.++ Explicit top-level keys win; ``extra_body`` is deep-merged ONE level so+ provider personality (e.g. ``thinking: {type: disabled}``) survives unless+ the explicit dict redefines that exact key. Both sides are deep-copied so+ transport-side mutation can't leak into the config/runtime cache. Returns+ None when both sides are empty.+ """+ import copy as _copy++ runtime_overrides = runtime_overrides if isinstance(runtime_overrides, dict) else None+ explicit_overrides = explicit_overrides if isinstance(explicit_overrides, dict) else None+ if not runtime_overrides and not explicit_overrides:+ return None+ merged = _copy.deepcopy(runtime_overrides) if runtime_overrides else {}+ explicit = _copy.deepcopy(explicit_overrides) if explicit_overrides else {}+ runtime_extra = merged.get("extra_body")+ explicit_extra = explicit.pop("extra_body", None)+ merged.update(explicit)+ if isinstance(runtime_extra, dict) and isinstance(explicit_extra, dict):+ runtime_extra.update(explicit_extra)+ merged["extra_body"] = runtime_extra+ elif explicit_extra is not None:+ merged["extra_body"] = explicit_extra+ return merged or None+++def _resolve_delegation_credentials(cfg: dict, parent_agent) -> dict:+ """Resolve the child credential bundle from the ``delegation`` config section.++ Three branches: ``base_url`` set → direct endpoint (``api_key`` None means+ inherit the parent's key, so providers keyed outside OPENAI_API_KEY work);+ ``provider`` set → full bundle via the runtime provider system (same path as+ CLI/gateway startup); neither → None values, child inherits everything.+ ``request_overrides`` is honored on every branch. Raises ValueError with a+ user-facing message on credential failure.+ """+ configured_model = str(cfg.get("model") or "").strip() or None+ configured_provider = str(cfg.get("provider") or "").strip() or None+ configured_base_url = str(cfg.get("base_url") or "").strip() or None+ configured_api_key = str(cfg.get("api_key") or "").strip() or None+ configured_api_mode = str(cfg.get("api_mode") or "").strip().lower() or None++ explicit_request_overrides = (+ cfg.get("request_overrides")+ if isinstance(cfg.get("request_overrides"), dict)+ else None+ )++ # Native-SDK providers speak their own wire protocol and can't be reached+ # via chat_completions against a base_url: always take the runtime-provider+ # path (a configured base_url still flows through it, e.g. a Bedrock region).+ _NATIVE_SDK_PROVIDERS = {"bedrock", "vertex", "google", "google-genai"}+ _provider_lower = (configured_provider or "").strip().lower()+ _is_native_sdk_provider = _provider_lower in _NATIVE_SDK_PROVIDERS++ if configured_base_url and not _is_native_sdk_provider:+ api_key = configured_api_key # None → inherited from parent in _build_child_agent++ # Shared URL-based api_mode detector so Anthropic-compatible direct+ # endpoints (/anthropic suffix: Azure AI Foundry, MiniMax, Zhipu, LiteLLM)+ # get the Messages transport instead of 404ing on chat_completions.+ from hermes_cli.runtime_provider import _detect_api_mode_for_url++ base_lower = configured_base_url.lower()+ provider = "custom"+ api_mode = _detect_api_mode_for_url(configured_base_url) or "chat_completions"+ if (+ base_url_hostname(configured_base_url) == "chatgpt.com"+ and "/backend-api/codex" in base_lower+ ):+ provider = "openai-codex"+ api_mode = "codex_responses"+ elif base_url_hostname(configured_base_url) == "api.anthropic.com":+ provider = "anthropic"+ api_mode = "anthropic_messages"+ elif "api.kimi.com/coding" in base_lower:+ provider = "custom"+ api_mode = "anthropic_messages"++ # Explicit delegation.api_mode always wins over the URL heuristic.+ if configured_api_mode in {"chat_completions", "codex_responses", "anthropic_messages"}:+ api_mode = configured_api_mode++ # provider configured ALONGSIDE base_url: pull that provider's request+ # personality (request_overrides / max_output_tokens) onto the explicit+ # endpoint. Best-effort — a resolution failure only skips the overrides.+ request_overrides = None+ max_output_tokens = None+ if configured_provider:+ try:+ from hermes_cli.runtime_provider import resolve_runtime_provider++ runtime = resolve_runtime_provider(+ requested=configured_provider, target_model=configured_model+ )+ request_overrides = dict(runtime.get("request_overrides") or {}) or None+ max_output_tokens = runtime.get("max_output_tokens")+ except Exception as exc:+ logger.debug(+ "delegation.base_url: runtime resolution for provider '%s' "+ "failed; proceeding without request_overrides: %s",+ configured_provider,+ exc,+ )++ request_overrides = _merge_request_overrides(+ request_overrides, explicit_request_overrides+ )++ return {+ "model": configured_model,+ "provider": provider,+ "base_url": configured_base_url,+ "api_key": api_key,+ "api_mode": api_mode,+ "request_overrides": request_overrides,+ "max_output_tokens": max_output_tokens,+ }++ if not configured_provider:+ # Pure inherit; explicit request_overrides still merge OVER the parent's.+ return {+ "model": configured_model,+ "provider": None,+ "base_url": None,+ "api_key": None,+ "api_mode": None,+ "request_overrides": _merge_request_overrides(+ getattr(parent_agent, "request_overrides", None),+ explicit_request_overrides,+ ),+ "max_output_tokens": None,+ }++ # Provider is configured — resolve full credentials+ try:+ from hermes_cli.runtime_provider import resolve_runtime_provider++ runtime = resolve_runtime_provider(requested=configured_provider, target_model=configured_model)+ 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"or set delegation.base_url/delegation.api_key for a direct endpoint. "+ 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 auth'."+ )++ # A pinned ACP transport command must exist — refuse loudly rather than let+ # the child silently fall back to another transport.+ pinned_command = runtime.get("command")+ if pinned_command:+ import shutil as _shutil++ if not _shutil.which(pinned_command):+ raise ValueError(+ f"Delegation provider '{configured_provider}' is pinned to the "+ f"'{pinned_command}' command, which was not found on PATH. "+ f"Install it or choose a different delegation provider."+ )++ return {+ "model": configured_model or runtime.get("model") or None,+ "provider": configured_provider if runtime.get("provider") == _RUNTIME_PROVIDER_CUSTOM else runtime.get("provider"),+ "base_url": runtime.get("base_url"),+ "api_key": api_key,+ "api_mode": runtime.get("api_mode"),+ "request_overrides": _merge_request_overrides(+ runtime.get("request_overrides"), explicit_request_overrides+ )+ or {},+ "max_output_tokens": runtime.get("max_output_tokens"),+ "command": runtime.get("command"),+ "args": list(runtime.get("args") or []),+ }+++def _load_config() -> dict:+ """Return the ``delegation`` config section (read-only — do NOT mutate).++ Prefers the shared ``load_config_readonly()`` (follows HERMES_HOME/profile;+ no deepcopy, since this runs on every get_definitions() rebuild) over the+ legacy ``cli.CLI_CONFIG``, which can hide user-set keys. Exception:+ ``HERMES_IGNORE_USER_CONFIG=1`` is only honored by the legacy loader, so it+ stays authoritative when that flag is set.+ """+ prefer_legacy = os.environ.get("HERMES_IGNORE_USER_CONFIG") == "1"+ if not prefer_legacy:+ try:+ from hermes_cli.config import load_config_readonly++ full = load_config_readonly()+ cfg = full.get("delegation") or {}+ if isinstance(cfg, dict):+ return cfg+ except Exception:+ pass+ try:+ from cli import CLI_CONFIG++ cfg = CLI_CONFIG.get("delegation") or {}+ return cfg if isinstance(cfg, dict) else {}+ except Exception:+ return {}diff --git a/tools/delegate_tool_dispatch.py b/tools/delegate_tool_dispatch.pynew file mode 100644index 00000000000..8eb674e1029--- /dev/null+++ b/tools/delegate_tool_dispatch.py@@ -0,0 +1,453 @@+"""Batch execution + background dispatch for delegate_task.++Split out of ``tools/delegate_tool.py`` (zero-back-ref regions of ``delegate_task``);+``delegate_task`` stays the orchestrator and calls these with its locals.+"""++from __future__ import annotations++import contextvars+import json+import logging+from typing import Any, Dict, List, Optional++from agent.interrupt_compat import request_hard_interrupt+from tools.delegate_tool_progress import (+ SUBAGENT_FAILURE_STATUSES,+ _clean_error_text,+ _emit_parent_console,+ format_batch_tag,+)++# Log-record parity with the origin module.+logger = logging.getLogger("tools.delegate_tool")+++def _fabricated_entry(idx: int, status: str, error: str, child: Any) -> Dict[str, Any]:+ """Result entry for a child whose Future raised or never finished."""+ return {+ "task_index": idx,+ "status": status,+ "summary": None,+ "error": error,+ "api_calls": 0,+ "duration_seconds": 0,+ "_child_role": getattr(child, "_delegate_role", None),+ }+++def _print_completion_line(parent_agent: Any, spinner_ref: Any, line: str) -> None:+ """Above-spinner line when a spinner exists (console fallback if it raises), else console."""+ if spinner_ref:+ try:+ spinner_ref.print_above(line)+ return+ except Exception:+ pass+ _emit_parent_console(parent_agent, f" {line}")+++def _run_children_parallel(+ children: List[tuple],+ results: list,+ *,+ parent_agent: Any,+ n_tasks: int,+ max_children: int,+ task_labels: List[str],+ live_deleg_id: Optional[str],+ honor_parent_interrupt: bool,+ origin_ui_session_id: str,+ origin_owner_transport: Any,+ origin_owner_session_record: Any,+) -> None:+ """Run a batch of built children in parallel, appending entries to ``results``.++ Polls futures with a short ``wait()`` timeout instead of ``as_completed()``+ so a wedged child cannot block the parent forever after an interrupt;+ on parent interrupt the still-pending children are reported as+ ``interrupted`` and abandoned. Prints one completion line per child.+ ``results`` ends sorted by task_index.+ """+ from tools.delegate_tool import _run_single_child++ _origin_ui_session_id = origin_ui_session_id+ _origin_owner_transport = origin_owner_transport+ _origin_owner_session_record = origin_owner_session_record+ # Batch -- run in parallel with per-task progress lines+ completed_count = 0+ spinner_ref = getattr(parent_agent, "_delegate_spinner", None)++ # Daemon workers (tools.daemon_pool): the `with` block still joins+ # normally, but if the parent is interrupted while a child is+ # wedged, the abandoned worker must not block interpreter exit.+ from tools.daemon_pool import DaemonThreadPoolExecutor+ with DaemonThreadPoolExecutor(max_workers=max_children) as executor:+ futures = {}+ for i, t, child in children:+ child_context = contextvars.copy_context()+ future = executor.submit(+ child_context.run,+ _run_single_child,+ task_index=i,+ goal=t["goal"],+ child=child,+ parent_agent=parent_agent,+ owner_session_id=_origin_ui_session_id or None,+ owner_transport=_origin_owner_transport,+ owner_session_record=_origin_owner_session_record,+ )+ futures[future] = i++ # Poll futures with interrupt checking. as_completed() blocks+ # until ALL futures finish — if a child agent gets stuck,+ # the parent blocks forever even after interrupt propagation.+ # Instead, use wait() with a short timeout so we can bail+ # when the parent is interrupted.+ # Map task_index -> child agent, so fabricated entries for+ # still-pending futures can carry the correct _delegate_role.+ _child_by_index = {i: child for (i, _, child) in children}++ pending = set(futures.keys())+ while pending:+ if (+ honor_parent_interrupt+ and getattr(parent_agent, "_interrupt_requested", False) is True+ ):+ # Parent interrupted — collect whatever finished and+ # abandon the rest. Children already received the+ # interrupt signal; we just can't wait forever.+ for f in pending:+ idx = futures[f]+ if f.done():+ try:+ entry = f.result()+ except Exception as exc:+ entry = _fabricated_entry(idx, "error", str(exc), _child_by_index.get(idx))+ else:+ entry = _fabricated_entry(+ idx,+ "interrupted",+ "Parent agent interrupted — child did not finish in time",+ _child_by_index.get(idx),+ )+ results.append(entry)+ completed_count += 1+ break++ from concurrent.futures import wait as _cf_wait, FIRST_COMPLETED++ done, pending = _cf_wait(+ pending, timeout=0.5, return_when=FIRST_COMPLETED+ )+ for future in done:+ try:+ entry = future.result()+ except Exception as exc:+ idx = futures[future]+ entry = _fabricated_entry(idx, "error", str(exc), _child_by_index.get(idx))+ results.append(entry)+ completed_count += 1++ # Print per-task completion line above the spinner+ 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+ _tag = format_batch_tag(live_deleg_id)+ _slot = f"{_tag} · {idx+1}/{n_tasks}" if _tag else f"{idx+1}/{n_tasks}"+ completion_line = f"{icon} [{_slot}] {label} ({dur}s)"+ # Failed/errored/timed-out children: say WHY on the+ # same line, cleaned to one short human-readable+ # fragment — a bare ✗ reads as "silently dropped".+ if status in SUBAGENT_FAILURE_STATUSES:+ _err_line = _clean_error_text(+ entry.get("error"), max_chars=120+ )+ if _err_line:+ completion_line += f" — {_err_line}"+ _print_completion_line(parent_agent, spinner_ref, completion_line)++ # Update spinner text to show remaining count+ if spinner_ref and remaining > 0:+ try:+ spinner_ref.update_text(+ f"🔀 {'[' + _tag + '] ' if _tag else ''}{remaining} task{'s' if remaining != 1 else ''} remaining"+ )+ except Exception as e:+ logger.debug("Spinner update_text failed: %s", e)++ # Sort by task_index so results match input order+ results.sort(key=lambda r: r["task_index"])+++def _dispatch_background(+ *,+ parent_agent: Any,+ context: Optional[str],+ task_list: List[Dict[str, Any]],+ children: List[tuple],+ creds: Dict[str, Any],+ top_role: str,+ live_deleg_id: Optional[str],+ live_paths: List[str],+ origin_wake_sid: str,+ origin_ui_session_id: str,+ execute_and_aggregate: Any,+) -> str:+ """Dispatch the WHOLE batch as one async unit and return the tool result JSON.++ ``execute_and_aggregate`` joins on every child and yields ONE consolidated+ results block that re-enters the conversation as a single message when ALL+ children finish. Falls back to running it synchronously (with an explanatory+ ``note``) when the session cannot receive detached completions or the async+ pool is at capacity.+ """+ from tools.delegate_tool import _get_max_async_children++ _execute_and_aggregate = execute_and_aggregate+ _origin_wake_sid = origin_wake_sid+ _origin_ui_session_id = origin_ui_session_id+ from tools.async_delegation import dispatch_async_delegation_batch+ from tools.approval import get_current_session_key++ # Finite sessions cannot route a detached subagent result back to the+ # agent after their turn/process ends. This includes stateless HTTP+ # requests (#10760) and one-shot Kanban workers (#63169). Fall back to+ # SYNCHRONOUS execution so the result returns in this same turn instead+ # of handing out a handle with no durable consumer. Mirrors the+ # pool-at-capacity inline fallback below.+ try:+ from gateway.session_context import async_delivery_supported+ _async_ok = async_delivery_supported()+ except Exception:+ _async_ok = True++ _wake_sid = ""+ if not _async_ok:+ # The adapter itself cannot push, but if a raw session id is+ # bound (the API server always binds one — see+ # ApiServerAdapter._bind_api_server_session), gateway.wake can+ # still reach the session by self-POSTing /v1/chat/completions+ # with that id in X-Hermes-Session-Id once the batch completes.+ # Only fall back to forced-sync execution when there is truly no+ # session id to wake. Uses the origin captured before child+ # construction (see _origin_wake_sid above) — reading+ # HERMES_SESSION_ID here would return the subagent's internal id.+ _wake_sid = _origin_wake_sid+ if _wake_sid:+ logger.info(+ "delegate_task: async delivery unsupported on this "+ "session, but a session id is bound (%s) — dispatching "+ "in the background and waking the session via self-post "+ "when it completes instead of forcing synchronous "+ "execution.",+ _wake_sid,+ )+ _async_ok = True++ if not _async_ok:+ logger.info(+ "delegate_task: async delivery unsupported on this session "+ "runtime; running the batch synchronously instead."+ )+ _sync_result = _execute_and_aggregate()+ if isinstance(_sync_result, dict):+ _sync_result["note"] = (+ "background=true is not available in this session — it cannot "+ "receive a detached subagent result after the turn ends (a "+ "one-shot runner such as `hermes -z`, a cron job, a Kanban "+ "worker, or a stateless HTTP endpoint). The subagent(s) ran "+ "SYNCHRONOUSLY and the result is included above."+ )+ return json.dumps(_sync_result, ensure_ascii=False)++ _session_key = get_current_session_key(default="")+ try:+ from gateway.session_context import get_session_env++ _source = get_session_env("HERMES_SESSION_SOURCE", "")+ # Refresh from the same task-local source when available, but retain+ # the immutable value captured before child construction otherwise.+ _origin_ui_session_id = (+ get_session_env("HERMES_UI_SESSION_ID", "") or _origin_ui_session_id+ )+ # In desktop/TUI, the routable session key is the durable+ # AIAgent.session_id. Context compression can rotate that id during+ # the same turn before the TUI-side session dict is re-anchored;+ # if we capture the stale approval/session context key here, the+ # async completion becomes an orphan and any desktop poller may+ # consume it. Gateway chats are different: their session_key is the+ # platform conversation key (agent:main:...), so keep it there.+ if _source == "tui":+ _agent_session_id = str(getattr(parent_agent, "session_id", "") or "")+ if _agent_session_id:+ _session_key = _agent_session_id+ except Exception:+ _source = ""+ if not _session_key:+ # CLI (single-process) path: the approval contextvar is only bound+ # during gateway/TUI turns and HERMES_SESSION_KEY is not in the CLI+ # environment, so the key resolves empty here. Since #64240 the CLI+ # drains completions through a positive-ownership filter keyed on+ # the durable AIAgent.session_id — an empty session_key would fail+ # closed and the CLI could never claim its own completions, while+ # a restored foreign event with an empty key could leak into any+ # unfiltered consumer (#64484). Stamp the parent's durable session+ # id instead; compression rotations are handled on the drain side+ # via resolve_resume_session_id lineage resolution.+ _agent_session_id = str(getattr(parent_agent, "session_id", "") or "")+ if _agent_session_id:+ _session_key = _agent_session_id+ _parent_session_id = getattr(parent_agent, "session_id", None)+ _child_agents = [c for (_, _, c) in children]++ # Detach every child from the parent's interrupt-propagation list — the+ # batch's lifecycle is owned by the async registry now, not the parent+ # turn. _build_child_agent attached them (correct for sync runs).+ if hasattr(parent_agent, "_active_children"):+ _ac_lock = getattr(parent_agent, "_active_children_lock", None)+ for _c in _child_agents:+ try:+ if _ac_lock:+ with _ac_lock:+ parent_agent._active_children.remove(_c)+ else:+ parent_agent._active_children.remove(_c)+ except ValueError:+ pass++ def _batch_runner():+ # This batch is detached from the foreground turn. Its lifecycle is+ # owned by the async registry and cancelled only via _batch_interrupt.+ return _execute_and_aggregate(honor_parent_interrupt=False)++ def _batch_interrupt():+ for _c in _child_agents:+ try:+ interrupted = request_hard_interrupt(_c, "Async delegation cancelled")+ if not interrupted and hasattr(_c, "_interrupt_requested"):+ _c._interrupt_requested = True+ except Exception:+ pass++ def _batch_progress():+ # Progress token for the async registry's stale monitor: the+ # combined (api_call_count, current_tool, last_activity_ts) of+ # every child. last_activity_ts is ticked by _touch_activity on+ # every streamed chunk ("receiving stream response"), every tool+ # transition, and every API-call start/completion — so a child+ # streaming a long response is alive even though api_call_count+ # only advances when the call completes (same liveness signal as+ # the compaction inactivity budget, PR #71508). A fully frozen+ # token past the stale threshold means the detached batch is+ # wedged (e.g. stuck inside the first model API call — #60203).+ # in_tool=True while ANY child is inside a tool so legitimately+ # slow tools get the higher staleness ceiling, mirroring the+ # sync-path heartbeat monitor.+ parts = []+ in_tool = False+ for _c in _child_agents:+ try:+ _summary = _c.get_activity_summary()+ _tool = _summary.get("current_tool")+ parts.append(+ (+ _summary.get("api_call_count", 0),+ _tool,+ _summary.get("last_activity_ts"),+ )+ )+ in_tool = in_tool or bool(_tool)+ except Exception:+ parts.append(None)+ return tuple(parts), in_tool++ _goals = [t["goal"] for t in task_list]+ dispatch = dispatch_async_delegation_batch(+ goals=_goals,+ context=context,+ # Metadata for the completion block only; subagents inherit the+ # parent's toolsets (no model-facing toolsets arg).+ toolsets=None,+ role=top_role,+ model=creds["model"],+ session_key=_session_key,+ origin_ui_session_id=_origin_ui_session_id,+ origin_session_id=_wake_sid,+ parent_session_id=_parent_session_id,+ runner=_batch_runner,+ interrupt_fn=_batch_interrupt,+ max_async_children=_get_max_async_children(),+ # Reuse the live-transcript directory's id (when created) so the+ # returned delegation_id matches cache/delegation/live/<id>/.+ delegation_id=live_deleg_id,+ progress_fn=_batch_progress,+ )++ if dispatch.get("status") == "dispatched":+ n = len(_goals)+ note = (+ "Subagent is running in the background. You and the user can "+ "keep working; its full result re-enters the conversation as a "+ "new message when it finishes. Do not wait or poll — just "+ "continue."+ if n == 1 else+ f"{n} subagents are running in parallel in the background. You "+ f"and the user can keep working; they wait on each other and "+ f"their consolidated results re-enter the conversation as a "+ f"single message once ALL of them finish. Do not wait or poll "+ f"— just continue."+ )+ payload = {+ "status": "dispatched",+ "mode": "background",+ "count": n,+ "delegation_id": dispatch["delegation_id"],+ "goals": _goals,+ "note": note,+ }+ _sids = [+ getattr(_c, "_subagent_id", None) for _c in _child_agents+ ]+ if any(isinstance(s, str) and s for s in _sids):+ payload["subagent_ids"] = _sids+ payload["control_hint"] = (+ "While a child runs you can orchestrate it live with this "+ "same tool: delegate_task(action='list') to see live "+ "children, action='steer' with subagent_id + message to "+ "redirect one, action='stop' with subagent_id to end one "+ "early."+ )+ if live_paths:+ payload["live_transcripts"] = list(live_paths)+ payload["live_transcripts_hint"] = (+ "Each subagent streams a human-readable transcript of its "+ "operations to the file listed above (append-only, one per "+ "task). Read or `tail -f` these paths at any time to watch "+ "a child work while it runs."+ )+ return json.dumps(payload, ensure_ascii=False)++ # Pool at capacity / schedule failure — children are still attached+ # (we detach above only on the parent list, but the async unit was+ # never accepted, so re-attaching isn't needed: we just run inline).+ logger.info(+ "delegate_task: async pool at capacity (%s); running the whole "+ "batch synchronously instead.",+ dispatch.get("error", "rejected"),+ )+ _cap_result = _execute_and_aggregate()+ if isinstance(_cap_result, dict):+ _cap_result["note"] = (+ "The background delegation pool was at capacity "+ "(delegation.max_concurrent_children), so the subagent(s) ran "+ "SYNCHRONOUSLY and the result is included above. Raise "+ "delegation.max_concurrent_children in config.yaml to allow "+ "more concurrent background delegations."+ )+ return json.dumps(_cap_result, ensure_ascii=False)diff --git a/tools/delegate_tool_progress.py b/tools/delegate_tool_progress.pynew file mode 100644index 00000000000..f70be4155eb--- /dev/null+++ b/tools/delegate_tool_progress.py@@ -0,0 +1,506 @@+"""Child progress relay, console formatting and child system-prompt construction for delegate_task.++Split out of ``tools/delegate_tool.py``; every moved name is re-imported there, so+``tools.delegate_tool.<name>`` keeps resolving (and monkeypatching) as before.+"""++from __future__ import annotations++import logging+import enum+import os+import threading+from typing import Any, Dict, List, Optional+from tools.delegate_tool_registry import _active_subagents, _active_subagents_lock++# Log-record parity with the origin module.+logger = logging.getLogger("tools.delegate_tool")++# Terminal child statuses that mean "the subagent did NOT deliver a usable+# result". Shared by the CLI spinner echo, the gateway failure notice, and+# the parent-facing failure summary so every surface agrees on what counts+# as a failure.+SUBAGENT_FAILURE_STATUSES = frozenset({"failed", "error", "timeout"})+++def _clean_error_text(error: Any, max_chars: int = 200) -> str:+ """Reduce an arbitrary error payload to one clean human-readable line.++ Provider/SDK errors routinely arrive as multi-line tracebacks or JSON+ walls. For a chat-facing notice we want the single most informative+ line: the exception message (last line of a traceback) or the first+ non-empty line otherwise, hard-capped in length.+ """+ text = str(error or "").strip()+ if not text:+ return ""+ lines = [ln.strip() for ln in text.splitlines() if ln.strip()]+ if not lines:+ return ""+ # A traceback's last line is the actual exception message.+ line = lines[-1] if lines[0].startswith("Traceback") else lines[0]+ if len(line) > max_chars:+ line = line[: max_chars - 3] + "..."+ return line+++def format_subagent_failure_line(+ goal: Optional[str],+ status: Optional[str],+ error: Any = None,+ duration_seconds: Any = None,+) -> str:+ """One clean, human-readable line describing a failed subagent.++ Rendered directly to the user (CLI spinner echo, gateway platform+ notice) — no JSON, no traceback, no internal field names. Example:++ ⚠️ Subagent failed — "research competitor pricing": Error code: 404 —+ model not found (after 12s)+ """+ goal_label = (goal or "").strip().replace("\n", " ")+ if len(goal_label) > 60:+ goal_label = goal_label[:57] + "..."+ verb = "timed out" if status == "timeout" else "failed"+ line = f"⚠️ Subagent {verb}"+ if goal_label:+ line += f' — "{goal_label}"'+ err = _clean_error_text(error)+ if err:+ line += f": {err}"+ if isinstance(duration_seconds, (int, float)) and duration_seconds > 0:+ line += f" (after {round(duration_seconds)}s)"+ return line+++class DelegateEvent(str, enum.Enum):+ """Formal event types emitted during delegation progress.++ _build_child_progress_callback normalises incoming legacy strings+ (``tool.started``, ``_thinking``, …) to these enum values via+ ``_LEGACY_EVENT_MAP``. External consumers (gateway SSE, ACP adapter,+ CLI) still receive the legacy strings during the deprecation window.++ TASK_SPAWNED / TASK_COMPLETED / TASK_FAILED are reserved for+ future orchestrator lifecycle events and are not currently emitted.+ """++ TASK_SPAWNED = "delegate.task_spawned"+ TASK_PROGRESS = "delegate.task_progress"+ TASK_COMPLETED = "delegate.task_completed"+ TASK_FAILED = "delegate.task_failed"+ TASK_THINKING = "delegate.task_thinking"+ TASK_TOOL_STARTED = "delegate.tool_started"+ TASK_TOOL_COMPLETED = "delegate.tool_completed"+++# Legacy event strings → DelegateEvent mapping.+# Incoming child-agent events use the old names; the callback normalises them.+_LEGACY_EVENT_MAP: Dict[str, DelegateEvent] = {+ "_thinking": DelegateEvent.TASK_THINKING,+ "reasoning.available": DelegateEvent.TASK_THINKING,+ "tool.started": DelegateEvent.TASK_TOOL_STARTED,+ "tool.completed": DelegateEvent.TASK_TOOL_COMPLETED,+ "subagent_progress": DelegateEvent.TASK_PROGRESS,+}+++def _build_child_system_prompt(+ goal: str,+ context: Optional[str] = None,+ *,+ workspace_path: Optional[str] = None,+ role: str = "leaf",+ max_spawn_depth: int = 2,+ child_depth: int = 1,+) -> str:+ """Build a focused system prompt for a child agent.++ When role='orchestrator', appends a delegation-capability block+ modeled on OpenClaw's buildSubagentSystemPrompt (canSpawn branch at+ inspiration/openclaw/src/agents/subagent-system-prompt.ts:63-95).+ The depth note is literal truth (grounded in the passed config) so+ the LLM doesn't confabulate nesting capabilities that don't exist.+ """+ 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}")+ if workspace_path and str(workspace_path).strip():+ parts.append(+ "\nWORKSPACE PATH:\n"+ f"{workspace_path}\n"+ "Use this exact path for local repository/workdir operations unless the task explicitly says otherwise."+ )+ # Project context files (AGENTS.md / CLAUDE.md / .cursorrules ...)+ # from the workspace, via the SAME discovery/priority/cap logic the+ # main agent's system prompt uses. Children are constructed with+ # skip_context_files=True (their prompt is this focused one), so+ # without this a subagent works in a repo without the repo's own+ # conventions unless it thinks to go read them. SOUL.md is skipped —+ # identity belongs to the parent. workspace_path comes only from+ # explicit sources (_resolve_workspace_hint: TERMINAL_CWD / agent cwd+ # hints, never bare getcwd), so the #64590 install-tree-fallback leak+ # doesn't apply here. Best-effort: on any failure the child prompt is+ # simply built without the block.+ try:+ from agent.prompt_builder import build_context_files_prompt++ _ctx_files = build_context_files_prompt(+ cwd=str(workspace_path), skip_soul=True+ )+ except Exception:+ logger.debug(+ "subagent: workspace context-files load failed", exc_info=True+ )+ _ctx_files = ""+ if _ctx_files.strip():+ parts.append(+ "\nThe workspace's project context files are reproduced "+ "below. Their conventions and invariants are binding for "+ "your work in this workspace.\n\n" + _ctx_files.strip()+ )+ 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"+ "Important workspace rule: Never assume a repository lives at /workspace/... or any other container-style path unless the task/context explicitly gives that path. "+ "If no exact local path is provided, discover it first before issuing git/workdir-specific commands.\n\n"+ "Keep your final summary tight: lead with outcomes, prefer bullet "+ "points over paragraphs, and don't replay your whole process. Your "+ "response is returned to the parent agent as a summary, and overlong "+ "summaries crowd out the parent's context window."+ )+ if role == "orchestrator":+ child_note = (+ "Your own children MUST be leaves (cannot delegate further) "+ "because they would be at the depth floor — you cannot pass "+ "role='orchestrator' to your own delegate_task calls."+ if child_depth + 1 >= max_spawn_depth+ else "Your own children can themselves be orchestrators or leaves, "+ "depending on the `role` you pass to delegate_task. Default is "+ "'leaf'; pass role='orchestrator' explicitly when a child "+ "needs to further decompose its work."+ )+ parts.append(+ "\n## Subagent Spawning (Orchestrator Role)\n"+ "You have access to the `delegate_task` tool and CAN spawn "+ "your own subagents to parallelize independent work.\n\n"+ "WHEN to delegate:\n"+ "- The goal decomposes into 2+ independent subtasks that can "+ "run in parallel (e.g. research A and B simultaneously).\n"+ "- A subtask is reasoning-heavy and would flood your context "+ "with intermediate data.\n\n"+ "WHEN NOT to delegate:\n"+ "- Single-step mechanical work — do it directly.\n"+ "- Trivial tasks you can execute in one or two tool calls.\n"+ "- Re-delegating your entire assigned goal to one worker "+ "(that's just pass-through with no value added).\n\n"+ "Coordinate your workers' results and synthesize them before "+ "reporting back to your parent. You are responsible for the "+ "final summary, not your workers.\n\n"+ f"NOTE: You are at depth {child_depth}. The delegation tree "+ f"is capped at max_spawn_depth={max_spawn_depth}. {child_note}"+ )+ return "\n".join(parts)+++def _resolve_workspace_hint(parent_agent) -> Optional[str]:+ """Best-effort local workspace hint for child prompts.++ We only inject a path when we have a concrete absolute directory. This avoids+ teaching subagents a fake container path while still helping them avoid+ guessing `/workspace/...` for local repo tasks.+ """+ candidates = [+ os.getenv("TERMINAL_CWD"),+ getattr(+ getattr(parent_agent, "_subdirectory_hints", None), "working_dir", None+ ),+ getattr(parent_agent, "terminal_cwd", None),+ getattr(parent_agent, "cwd", None),+ ]+ for candidate in candidates:+ if not candidate:+ continue+ try:+ text = os.path.abspath(os.path.expanduser(str(candidate)))+ except Exception:+ continue+ if os.path.isabs(text) and os.path.isdir(text):+ return text+ return None+++_BATCH_ORDINALS: Dict[str, int] = {}+_BATCH_ORDINALS_LOCK = threading.Lock()+++def format_batch_tag(delegation_id: Optional[str]) -> str:+ """Short human tag identifying which delegation batch a line belongs to.++ ``deleg_6a664903`` → ``set 1`` (first batch seen in this process),+ the next distinct id → ``set 2``, and so on. Several batches (a parent's+ fan-out plus a child's nested fan-out, or two concurrent tools) print+ interleaved ``[n/N]`` progress lines to the same console; without a batch+ tag a ``✓ [3/3]`` and a ``✓ [3/9]`` are indistinguishable, and a raw hex+ slice (``[b2ac 3/9]``) is attributable but unreadable. Empty string when+ no id is known so callers can concatenate unconditionally.+ """+ if not isinstance(delegation_id, str) or not delegation_id:+ return ""+ with _BATCH_ORDINALS_LOCK:+ n = _BATCH_ORDINALS.get(delegation_id)+ if n is None:+ n = len(_BATCH_ORDINALS) + 1+ _BATCH_ORDINALS[delegation_id] = n+ return f"set {n}"+++def _batch_prefix(delegation_id: Optional[str], task_index: int, task_count: int) -> str:+ """``[set 2 · 3/9] `` for batch children, ``[set 2] `` for a lone child,+ ``[3/9] `` / ``""`` when the batch id is unknown."""+ tag = format_batch_tag(delegation_id)+ if task_count > 1:+ inner = f"{tag} · {task_index + 1}/{task_count}" if tag else f"{task_index + 1}/{task_count}"+ return f"[{inner}] "+ return f"[{tag}] " if tag else ""+++def _emit_parent_console(parent_agent, line: str) -> None:+ """Emit a human-readable progress line to the parent's console.++ Routes through ``parent_agent._safe_print`` when available so headless+ stdio hosts (ACP, gateway API) can redirect non-protocol output to+ stderr via their configured ``_print_fn``. A bare ``print()`` would+ otherwise land on stdout and corrupt JSON-RPC framing.+ """+ printer = getattr(parent_agent, "_safe_print", None)+ if callable(printer):+ try:+ printer(line)+ return+ except Exception:+ pass+ print(line)+++def _build_child_progress_callback(+ task_index: int,+ goal: str,+ parent_agent,+ task_count: int = 1,+ *,+ subagent_id: Optional[str] = None,+ parent_id: Optional[str] = None,+ depth: Optional[int] = None,+ model: Optional[str] = None,+ toolsets: Optional[List[str]] = None,+ session_ref: Optional[Dict[str, Any]] = None,+) -> Optional[callable]:+ """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++ The identity kwargs (``subagent_id``, ``parent_id``, ``depth``, ``model``,+ ``toolsets``) are threaded into every relayed event so the TUI can+ reconstruct the live spawn tree and route per-branch controls (kill,+ pause) back by ``subagent_id``. All are optional for backward compat —+ older callers that ignore them still produce a flat list on the TUI.++ 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++ # Show 1-indexed prefix only in batch mode (multiple tasks). The batch tag+ # (short delegation id) is resolved lazily from session_ref because the+ # callback is built before delegate_task stamps ``_delegation_id`` on the+ # child; delegate_task drops the id into the same shared ref.+ def _prefix() -> str:+ deleg = session_ref.get("delegation_id") if session_ref else None+ return _batch_prefix(deleg, task_index, task_count)++ goal_label = (goal or "").strip()++ # Gateway: batch tool names, flush periodically+ _BATCH_SIZE = 5+ _batch: List[str] = []+ _tool_count = [0] # per-subagent running counter (list for closure mutation)++ def _identity_kwargs() -> Dict[str, Any]:+ kw: Dict[str, Any] = {+ "task_index": task_index,+ "task_count": task_count,+ "goal": goal_label,+ }+ if subagent_id is not None:+ kw["subagent_id"] = subagent_id+ if parent_id is not None:+ kw["parent_id"] = parent_id+ if depth is not None:+ kw["depth"] = depth+ if model is not None:+ kw["model"] = model+ if toolsets is not None:+ kw["toolsets"] = list(toolsets)+ # The child's own session id — filled into the shared ref once the+ # child agent exists (the callback is built first), so every relayed+ # event lets UIs open/inspect the subagent's session directly.+ if session_ref and session_ref.get("session_id"):+ kw["child_session_id"] = str(session_ref["session_id"])+ if session_ref and session_ref.get("delegation_id"):+ kw["delegation_id"] = str(session_ref["delegation_id"])+ kw["tool_count"] = _tool_count[0]+ return kw++ def _relay(+ event_type: str, tool_name: str = None, preview: str = None, args=None, **kwargs+ ):+ if not parent_cb:+ return+ payload = _identity_kwargs()+ payload.update(kwargs) # caller overrides (e.g. status, duration_seconds)+ try:+ parent_cb(event_type, tool_name, preview, args, **payload)+ except Exception as e:+ logger.debug("Parent callback failed: %s", e)++ def _tree_line(text: str) -> None:+ """Print one tree-view line above the CLI spinner (no-op without a spinner)."""+ if not spinner:+ return+ try:+ spinner.print_above(f" {_prefix()}├─ {text}")+ except Exception as e:+ logger.debug("Spinner print_above failed: %s", e)++ def _flush_batch() -> None:+ if parent_cb and _batch:+ _relay("subagent.progress", preview=f"🔀 {_prefix()}{', '.join(_batch)}")+ _batch.clear()++ def _short(text: str, n: int) -> str:+ return (text[:n] + "...") if len(text) > n else text++ # ── Lifecycle events emitted by the orchestrator itself (not DelegateEvent) ──+ def _on_start(tool_name, preview, args, kwargs):+ if goal_label:+ _tree_line(f"🔀 {_short(goal_label, 55)}")+ _relay("subagent.start", preview=preview or goal_label or "", **kwargs)++ def _on_complete(tool_name, preview, args, kwargs):+ # Failed child: echo one clean reason line into the CLI tree so the human+ # sees WHY, not just a vanished branch. Gateway-side rendering happens in+ # TurnRunner.progress_callback off the relayed event.+ if kwargs.get("status") in SUBAGENT_FAILURE_STATUSES:+ _tree_line(+ format_subagent_failure_line(+ goal_label,+ kwargs.get("status"),+ error=kwargs.get("summary") or preview,+ duration_seconds=kwargs.get("duration_seconds"),+ )+ )+ _relay("subagent.complete", preview=preview, **kwargs)++ def _on_text(tool_name, preview, args, kwargs):+ # Streamed child reply text, relayed verbatim so a gateway watch window+ # mirrors the child "talking". No spinner echo: CLI/TUI progress+ # handlers ignore non-tool events, so this is inert there.+ _relay("subagent.text", preview=preview)++ # ── DelegateEvent handlers ──+ def _on_thinking(tool_name, preview, args, kwargs):+ text = preview or tool_name or ""+ _tree_line(f'💭 "{_short(text, 55)}"')+ _relay("subagent.thinking", preview=text)++ def _on_progress(tool_name, preview, args, kwargs):+ # Pre-batched summary from a nested orchestrator's grandchild; upstream+ # emits parent_cb("subagent_progress", summary) with the summary in the+ # tool_name slot. Pass through: render distinctly (not via the tool+ # emoji lookup) and relay upward without re-batching.+ summary_text = tool_name or preview or ""+ if summary_text:+ _tree_line(f"🔀 {summary_text}")+ if parent_cb:+ try:+ parent_cb("subagent_progress", f"{_prefix()}{summary_text}")+ except Exception as e:+ logger.debug("Parent callback relay failed: %s", e)++ def _on_tool_started(tool_name, preview, args, kwargs):+ _tool_count[0] += 1+ if subagent_id is not None:+ with _active_subagents_lock:+ rec = _active_subagents.get(subagent_id)+ if rec is not None:+ rec["tool_count"] = _tool_count[0]+ rec["last_tool"] = tool_name or ""+ if spinner:+ from agent.display import get_tool_emoji++ line = f"{get_tool_emoji(tool_name or '')} {tool_name}"+ short = _short(preview, 35) if preview else ""+ if short:+ line += f' "{short}"'+ _tree_line(line)+ if parent_cb:+ _relay("subagent.tool", tool_name, preview, args)+ _batch.append(tool_name or "")+ if len(_batch) >= _BATCH_SIZE:+ _flush_batch()++ _lifecycle_handlers = {+ "subagent.start": _on_start,+ "subagent.complete": _on_complete,+ "subagent.text": _on_text,+ }+ # Any other DelegateEvent (TASK_TOOL_STARTED and the reserved TASK_* values)+ # takes the tool-started path, as before the table.+ _event_handlers = {+ DelegateEvent.TASK_THINKING: _on_thinking,+ DelegateEvent.TASK_PROGRESS: _on_progress,+ DelegateEvent.TASK_TOOL_COMPLETED: None,+ }++ def _callback(+ event_type, tool_name: str = None, preview: str = None, args=None, **kwargs+ ):+ handler = _lifecycle_handlers.get(event_type) if isinstance(event_type, str) else None+ if handler is None:+ # Normalise legacy strings, "delegate.*" strings and DelegateEvent+ # values to one DelegateEvent; unknown events are ignored.+ if isinstance(event_type, DelegateEvent):+ event = event_type+ else:+ event = _LEGACY_EVENT_MAP.get(event_type)+ if event is None:+ try:+ event = DelegateEvent(event_type)+ except (ValueError, TypeError):+ return+ handler = _event_handlers.get(event, _on_tool_started)+ if handler is None:+ return+ handler(tool_name, preview, args, kwargs)++ def _flush():+ """Flush remaining batched tool names to gateway on completion."""+ _flush_batch()++ _callback._flush = _flush+ return _callbackdiff --git a/tools/delegate_tool_registry.py b/tools/delegate_tool_registry.pynew file mode 100644index 00000000000..a6e1df794aa--- /dev/null+++ b/tools/delegate_tool_registry.py@@ -0,0 +1,457 @@+"""Live-subagent registry + model-facing control plane (list/steer/stop) for delegate_task.++Split out of ``tools/delegate_tool.py``; every moved name is re-imported there, so+``tools.delegate_tool.<name>`` keeps resolving (and monkeypatching) as before.+"""++from __future__ import annotations++import logging+import json+import threading+import time+from typing import Any, Dict, List, Optional+from agent.interrupt_compat import request_hard_interrupt+from tools.registry import tool_error++# Log-record parity with the origin module.+logger = logging.getLogger("tools.delegate_tool")++_spawn_pause_lock = threading.Lock()++_spawn_paused: bool = False++_active_subagents_lock = threading.Lock()++# subagent_id -> mutable record tracking the live child agent. Stays only+# for the lifetime of the run; _run_single_child is the owner.+_active_subagents: Dict[str, Dict[str, Any]] = {}++# subagent_id -> {goal, delegation_id, parent_session_id} retained AFTER the+# child finishes (bounded FIFO). Child-started background processes routinely+# outlive the child itself (its npm ci with notify_on_complete=true finishes+# after the child's summary was delivered); their completion notifications+# reach the parent conversation via the shared completion_queue and need+# delegation attribution even though the live registry entry is gone.+_RECENT_SUBAGENTS_CAP = 200++_recent_subagents: Dict[str, Dict[str, Any]] = {}+++def get_subagent_attribution(task_id: Optional[str]) -> Optional[Dict[str, Any]]:+ """Resolve a process task_id to its originating delegation, if any.++ Children run their terminal sessions under ``task_id == subagent_id``+ (see _run_single_child's child_task_id), so a background process spawned+ by a subagent carries that id in ``ProcessSession.task_id``. Returns+ ``{subagent_id, goal, delegation_id}`` for live AND recently-finished+ children, or None when the task_id is not a known subagent.+ """+ if not task_id or not isinstance(task_id, str):+ return None+ with _active_subagents_lock:+ record = _active_subagents.get(task_id)+ if record is not None:+ return {+ "subagent_id": task_id,+ "goal": record.get("goal"),+ "delegation_id": record.get("delegation_id"),+ }+ retained = _recent_subagents.get(task_id)+ if retained is not None:+ return {+ "subagent_id": task_id,+ "goal": retained.get("goal"),+ "delegation_id": retained.get("delegation_id"),+ }+ return None+++def set_spawn_paused(paused: bool) -> bool:+ """Globally block/unblock new delegate_task spawns.++ Active children keep running; only NEW calls to delegate_task fail fast+ with a "spawning paused" error until unblocked. Returns the new state.+ """+ global _spawn_paused+ with _spawn_pause_lock:+ _spawn_paused = bool(paused)+ return _spawn_paused+++def is_spawn_paused() -> bool:+ with _spawn_pause_lock:+ return _spawn_paused+++def _register_subagent(record: Dict[str, Any]) -> None:+ sid = record.get("subagent_id")+ if not sid:+ return+ record.setdefault("accepting_steer", True)+ with _active_subagents_lock:+ _active_subagents[sid] = record+++def _retain_recent_subagent(record: Dict[str, Any]) -> None:+ """Keep a bounded attribution stub after a child finishes (lock held)."""+ sid = record.get("subagent_id")+ if not sid:+ return+ _recent_subagents[sid] = {+ "goal": record.get("goal"),+ "delegation_id": record.get("delegation_id"),+ "owner_agent_session_id": record.get("owner_agent_session_id"),+ }+ while len(_recent_subagents) > _RECENT_SUBAGENTS_CAP:+ _recent_subagents.pop(next(iter(_recent_subagents)), None)+++def _unregister_subagent(subagent_id: str, *, agent: Any = None) -> None:+ with _active_subagents_lock:+ record = _active_subagents.get(subagent_id)+ if record is not None and (agent is None or record.get("agent") is agent):+ _active_subagents.pop(subagent_id, None)+ _retain_recent_subagent(record)+++def _close_subagent_steering(subagent_id: str, agent: Any) -> Optional[str]:+ """Atomically close steer acceptance and drain its final durable artifact.++ ``steer_subagent`` holds the same registry lock through ``agent.steer``.+ Therefore either acceptance wins and this drain sees its exact text, or+ closure wins and the caller is rejected. Exact agent identity prevents a+ finishing child with a recycled public id from closing its replacement.+ """+ with _active_subagents_lock:+ record = _active_subagents.get(subagent_id)+ if record is None or record.get("agent") is not agent:+ return None+ record["accepting_steer"] = False+ drain = getattr(agent, "_drain_pending_steer", None)+ if not callable(drain):+ return None+ try:+ pending = drain()+ except Exception as exc:+ logger.debug("final steer drain for %s failed: %s", subagent_id, exc)+ return None+ return pending if isinstance(pending, str) and pending.strip() else None+++def interrupt_subagent(subagent_id: str) -> bool:+ """Request that a single running subagent stop at its next iteration boundary.++ Does not hard-kill the worker thread (Python can't); sets the child's+ interrupt flag which propagates to in-flight tools and recurses into+ grandchildren via AIAgent.interrupt(). Returns True if a matching+ subagent was found.+ """+ with _active_subagents_lock:+ record = _active_subagents.get(subagent_id)+ if not record:+ return False+ agent = record.get("agent")+ if agent is None:+ return False+ try:+ if not request_hard_interrupt(agent, f"Interrupted via TUI ({subagent_id})"):+ return False+ except Exception as exc:+ logger.debug("interrupt_subagent(%s) failed: %s", subagent_id, exc)+ return False+ return True+++def steer_subagent(+ subagent_id: str,+ text: str,+ *,+ owner_session_id: Optional[str] = None,+ owner_transport: Any = None,+ owner_session_record: Any = None,+) -> bool:+ """Queue steering text into a single running subagent without stopping it.++ The redirection-side mirror of interrupt_subagent(): resolves the live+ child in the registry and calls AIAgent.steer(), which appends the text+ to the child's last tool result at its next iteration boundary — the+ current tool call is never cut. Returns True if a matching subagent+ QUEUED the text while the child was still accepting work; False for an+ unknown/closed id, an ownership mismatch, a record with no live agent, or+ empty text. ``owner_session_id=None`` deliberately preserves the internal+ in-process helper contract; gateway callers must pass exact authority.++ Acceptance and completion are linearized by the registry lock. If+ acceptance wins but no delivery boundary remains, ``_run_single_child``+ drains the exact text into the completion entry as ``missed_steer``.+ """+ if not text or not text.strip():+ return False+ with _active_subagents_lock:+ record = _active_subagents.get(subagent_id)+ if not record or not record.get("accepting_steer", False):+ return False+ if owner_session_id is not None and (+ record.get("owner_session_id") != owner_session_id+ or owner_transport is None+ or record.get("owner_transport") is not owner_transport+ or owner_session_record is None+ or record.get("owner_session_record") is not owner_session_record+ ):+ return False+ agent = record.get("agent")+ if agent is None:+ return False+ try:+ return bool(agent.steer(text))+ except Exception as exc:+ logger.debug("steer_subagent(%s) failed: %s", subagent_id, exc)+ return False+++def _capture_gateway_steer_authority(+ owner_session_id: Optional[str],+) -> tuple[Any, Any]:+ """Capture exact request transport + live session generation, if any.++ This is intentionally an in-process bridge, not a serializable capability.+ Non-gateway hosts (including the CLI helper path) receive ``(None, None)``.+ """+ if not owner_session_id:+ return None, None+ try:+ from tui_gateway.server import _current_session_steer_authority++ return _current_session_steer_authority(owner_session_id)+ except Exception:+ return None, None+++def list_active_subagents() -> List[Dict[str, Any]]:+ """Snapshot of the currently running subagent tree.++ Each record: {subagent_id, parent_id, depth, goal, model, started_at,+ tool_count, status}. Safe to call from any thread — returns a copy.+ """+ with _active_subagents_lock:+ return [+ {+ k: v+ for k, v in r.items()+ if k+ not in {+ "agent",+ "owner_session_id",+ "owner_transport",+ "owner_session_record",+ "accepting_steer",+ }+ }+ for r in _active_subagents.values()+ ]+++def _is_descendant_of(child_agent: Any, parent_agent: Any, max_hops: int = 8) -> bool:+ """True when *child_agent* sits below *parent_agent* in the spawn tree.++ Walks the ``_delegate_parent_ref`` weakref chain stamped at build time.+ Identity comparison only — a parent may steer/stop its own children and+ grandchildren, never a sibling tree owned by another conversation.+ """+ if child_agent is None or parent_agent is None:+ return False+ cur = child_agent+ for _ in range(max_hops):+ ref = getattr(cur, "_delegate_parent_ref", None)+ ancestor = ref() if callable(ref) else None+ if ancestor is None:+ return False+ if ancestor is parent_agent:+ return True+ cur = ancestor+ return False+++# Model-facing control actions accepted by delegate_task(action=...).+# "spawn" (or omitted) keeps the historical spawn semantics.+_CONTROL_ACTIONS = frozenset({"list", "steer", "stop"})+++def _resolve_session_lineage(session_id: Optional[str], parent_agent: Any) -> str:+ """Resolve a session id to the tip of its compression lineage.++ Best-effort: uses the parent's live SessionDB handle when present so a+ delegation dispatched before a compression rotation still matches the+ rotated parent. Returns the input unchanged when resolution fails.+ """+ sid = str(session_id or "")+ if not sid:+ return ""+ db = getattr(parent_agent, "_session_db", None)+ if db is None:+ return sid+ try:+ resolved = db.resolve_resume_session_id(sid)+ return str(resolved) if resolved else sid+ except Exception:+ return sid+++def _owns_subagent_record(record: Dict[str, Any], parent_agent: Any) -> bool:+ """True when *parent_agent*'s conversation owns this live-child record.++ Two-tier check:++ 1. Object identity — the ``_delegate_parent_ref`` weakref chain stamped+ at build time reaches *parent_agent*. Fast path for the common case+ where the parent AIAgent object survives the whole run.+ 2. Durable conversation lineage — the child was registered with the+ owning conversation's durable session id+ (``owner_agent_session_id``); match it against the calling parent's+ ``session_id``, resolving compression-rotation lineage on both sides.++ Tier 2 exists because the identity chain is BRITTLE across parent-agent+ rebuilds: the CLI sets ``self.agent = None`` mid-session (route-signature+ change, credential refresh, /model, MoA one-shots) and constructs a NEW+ AIAgent for the next turn while the child keeps running with a weakref to+ the old object. The delivery path always survived this (it routes by+ durable session id); the control path must use the same durable spine or+ running children go invisible/unsteerable (observed live: deleg_88454b70+ / sa-0-dc0100f4, 2026-08-17).+ """+ agent = record.get("agent")+ if _is_descendant_of(agent, parent_agent):+ return True+ owner_sid = str(record.get("owner_agent_session_id") or "")+ if not owner_sid:+ return False+ parent_sid = str(getattr(parent_agent, "session_id", "") or "")+ if not parent_sid:+ return False+ if owner_sid == parent_sid:+ return True+ # Compression rotation on either side: compare lineage tips.+ return _resolve_session_lineage(owner_sid, parent_agent) in {+ parent_sid,+ _resolve_session_lineage(parent_sid, parent_agent),+ }+++def _handle_control_action(+ action: str,+ subagent_id: Optional[str],+ message: Optional[str],+ parent_agent: Any,+) -> str:+ """Synchronous control plane for delegate_task: list/steer/stop.++ Runs in-turn (never backgrounded) and only over subagents descended from+ *parent_agent* — the same registry the TUI overlay drives, but scoped so+ a conversation can only control its own spawn tree.+ """+ if action == "list":+ with _active_subagents_lock:+ records = list(_active_subagents.values())+ entries = []+ for r in records:+ agent = r.get("agent")+ if not _owns_subagent_record(r, parent_agent):+ continue+ started = r.get("started_at")+ entries.append(+ {+ "subagent_id": r.get("subagent_id"),+ "parent_id": r.get("parent_id"),+ "goal": r.get("goal"),+ "model": r.get("model"),+ "status": r.get("status"),+ "running_seconds": (+ round(time.time() - started, 1)+ if isinstance(started, (int, float))+ else None+ ),+ "accepting_steer": bool(r.get("accepting_steer", False)),+ "live_transcript": getattr(agent, "_live_transcript_path", None),+ }+ )+ payload: Dict[str, Any] = {+ "action": "list",+ "count": len(entries),+ "subagents": entries,+ }+ if not entries:+ payload["note"] = (+ "No live subagents right now. Children that already finished "+ "have delivered (or will deliver) their results as normal "+ "completion messages — there is nothing to steer or stop."+ )+ return json.dumps(payload, ensure_ascii=False)++ # steer / stop need a resolvable, owned target.+ sid = (subagent_id or "").strip()+ if not sid:+ return tool_error(+ f"action='{action}' requires subagent_id (from the spawn dispatch "+ "response or action='list')."+ )+ with _active_subagents_lock:+ record = _active_subagents.get(sid)+ if record is None or not _owns_subagent_record(record, parent_agent):+ return tool_error(+ f"No live subagent '{sid}' in this conversation's spawn tree. It "+ "may have already finished (its result arrives as a normal "+ "completion message). Use action='list' to see live children."+ )++ if action == "stop":+ if interrupt_subagent(sid):+ return json.dumps(+ {+ "action": "stop",+ "subagent_id": sid,+ "status": "interrupt_requested",+ "note": (+ "The subagent stops at its next iteration boundary "+ "(in-flight tool calls are asked to cancel). Its "+ "partial result still re-enters the conversation as a "+ "completion message — do not wait or poll."+ ),+ },+ ensure_ascii=False,+ )+ return tool_error(+ f"Could not interrupt '{sid}' — it likely finished in the last "+ "moment. Its result arrives as a normal completion message."+ )++ if action == "steer":+ text = (message or "").strip()+ if not text:+ return tool_error(+ "action='steer' requires a non-empty 'message' describing the "+ "course correction."+ )+ if steer_subagent(sid, text):+ return json.dumps(+ {+ "action": "steer",+ "subagent_id": sid,+ "status": "queued",+ "note": (+ "Steering text queued. The subagent sees it appended "+ "to its next tool result — the current tool call is "+ "never cut. If the child finishes before a delivery "+ "boundary remains, the text is reported back as "+ "missed_steer in its completion entry."+ ),+ },+ ensure_ascii=False,+ )+ return tool_error(+ f"Subagent '{sid}' is no longer accepting steering (finishing or "+ "already finished). Its result arrives as a normal completion "+ "message; re-delegate a follow-up task if more work is needed."+ )++ return tool_error(f"Unknown action '{action}'. Use spawn, list, steer, or stop.")diff --git a/tools/delegate_tool_results.py b/tools/delegate_tool_results.pynew file mode 100644index 00000000000..39635063142--- /dev/null+++ b/tools/delegate_tool_results.py@@ -0,0 +1,602 @@+"""Subagent result post-processing: summary budget/spill, tool-trace summaries, lifecycle hooks and cost rollup.++Split out of ``tools/delegate_tool.py``; every moved name is re-imported there, so+``tools.delegate_tool.<name>`` keeps resolving (and monkeypatching) as before.+"""++from __future__ import annotations++import logging+import json+import threading+from typing import Any, Dict, List, Optional+from urllib.parse import urlsplit, urlunsplit++# Log-record parity with the origin module.+logger = logging.getLogger("tools.delegate_tool")+++def _extract_output_tail(+ result: Dict[str, Any],+ *,+ max_entries: int = 12,+ max_chars: int = 8000,+) -> List[Dict[str, Any]]:+ """Pull the last N tool-call results from a child's conversation.++ Powers the overlay's "Output" section — the cc-swarm-parity feature.+ We reuse the same messages list the trajectory saver walks, taking+ only the tail to keep event payloads small. Each entry is+ ``{tool, preview, is_error}``.+ """+ messages = result.get("messages") if isinstance(result, dict) else None+ if not isinstance(messages, list):+ return []++ # Walk in reverse to build a tail; stop when we have enough.+ tail: List[Dict[str, Any]] = []+ pending_call_by_id: Dict[str, str] = {}++ # First pass (forward): build tool_call_id -> tool_name map+ for msg in messages:+ if not isinstance(msg, dict):+ continue+ if msg.get("role") == "assistant":+ for tc in msg.get("tool_calls") or []:+ tc_id = tc.get("id")+ fn = tc.get("function") or {}+ if tc_id:+ pending_call_by_id[tc_id] = str(fn.get("name") or "tool")++ # Second pass (reverse): pick tool results, newest first+ for msg in reversed(messages):+ if len(tail) >= max_entries:+ break+ if not isinstance(msg, dict) or msg.get("role") != "tool":+ continue+ # Flatten content-block lists/dicts to text so the overlay shows real+ # output (not a "[{'type': 'text'...}]" blob) and error detection can+ # see markers buried inside content blocks. Crude str() here would+ # mislabel a block-wrapped "Error: ..." result as is_error=False.+ content = _stringify_tool_content(msg.get("content") or "")+ is_error = _looks_like_error_output(content)+ tool_name = pending_call_by_id.get(msg.get("tool_call_id") or "", "tool")+ # Preserve line structure so the overlay's wrapped scroll region can+ # show real output rather than a whitespace-collapsed blob. We still+ # cap the payload size to keep events bounded.+ preview = content[:max_chars]+ tail.append({"tool": tool_name, "preview": preview, "is_error": is_error})++ tail.reverse() # restore chronological order for display+ return tail+++def _stringify_tool_content(content: Any) -> str:+ """Return a stable text representation for tool-result content.++ Most providers store tool results as strings, but some OpenAI-compatible+ paths can return content-block lists. Delegate observability must never+ crash while summarising a child run just because the transport used blocks.+ """+ if content is None:+ return ""+ if isinstance(content, str):+ return content+ if isinstance(content, list):+ parts = []+ for item in content:+ if isinstance(item, dict):+ text = item.get("text")+ if isinstance(text, str):+ parts.append(text)+ else:+ parts.append(json.dumps(item, ensure_ascii=False, default=str))+ else:+ parts.append(str(item))+ return "\n".join(parts)+ if isinstance(content, dict):+ return json.dumps(content, ensure_ascii=False, default=str)+ return str(content)+++_TOOL_INPUT_TARGET_KEYS = frozenset({+ "cwd",+ "destination_path",+ "directory",+ "dst",+ "endpoint",+ "file_path",+ "new_path",+ "old_path",+ "path",+ "source_path",+ "src",+ "target_path",+ "url",+ "urls",+})++_TOOL_INPUT_URL_KEYS = frozenset({"endpoint", "url", "urls"})+++def _sanitize_tool_target(key: str, value: Any) -> Any:+ """Keep bounded side-effect targets while dropping URL secrets."""+ if isinstance(value, list):+ cleaned = [+ item for item in (_sanitize_tool_target(key, item) for item in value[:16])+ if item is not None+ ]+ return cleaned or None+ if not isinstance(value, str) or not value:+ return None+ bounded = value[:1024]+ if key in _TOOL_INPUT_URL_KEYS:+ try:+ parsed = urlsplit(bounded)+ if parsed.scheme and parsed.netloc:+ hostname = parsed.hostname+ if not hostname:+ return None+ # ``SplitResult.netloc`` includes ``user:password@``. Rebuild+ # the authority from parsed host/port so hook-visible history+ # cannot carry URL credentials. Bracket IPv6 literals before+ # appending a validated port.+ host = f"[{hostname}]" if ":" in hostname else hostname+ port = parsed.port+ netloc = f"{host}:{port}" if port is not None else host+ return urlunsplit((parsed.scheme, netloc, parsed.path, "", ""))+ except ValueError:+ return None+ return bounded+++def _empty_input_summary() -> Dict[str, Any]:+ return {"argument_keys": [], "targets": {}}+++def _sanitize_targets(mapping: Dict[str, Any]) -> Dict[str, Any]:+ """Keep only known side-effect target keys, each sanitized (URL secrets dropped)."""+ targets: Dict[str, Any] = {}+ for raw_key, value in mapping.items():+ key = str(raw_key).lower()+ if key in _TOOL_INPUT_TARGET_KEYS:+ cleaned = _sanitize_tool_target(key, value)+ if cleaned is not None:+ targets[key] = cleaned+ return targets+++def _summarize_tool_arguments(arguments: Any) -> Dict[str, Any]:+ """Summarize argument names and side-effect targets without raw payloads."""+ if not isinstance(arguments, str):+ return _empty_input_summary()+ try:+ parsed = json.loads(arguments)+ except (TypeError, ValueError):+ return _empty_input_summary()+ if not isinstance(parsed, dict):+ return _empty_input_summary()+ keys = sorted(str(key)[:128] for key in parsed)[:64]+ return {"argument_keys": keys, "targets": _sanitize_targets(parsed)}+++def _sanitize_tool_input_summary(summary: Any) -> Dict[str, Any]:+ """Re-sanitize a stored input summary before handing it to lifecycle hooks."""+ if not isinstance(summary, dict):+ return _empty_input_summary()+ keys = summary.get("argument_keys")+ safe_keys = [str(key)[:128] for key in keys[:64]] if isinstance(keys, list) else []+ targets = summary.get("targets")+ safe_targets = _sanitize_targets(targets) if isinstance(targets, dict) else {}+ return {"argument_keys": safe_keys, "targets": safe_targets}+++def _subagent_stop_tool_call_history(tool_trace: Any) -> List[Dict[str, Any]]:+ """Build a detached, metadata-only tool history for lifecycle hooks."""+ if not isinstance(tool_trace, list):+ return []++ history: List[Dict[str, Any]] = []+ for item in tool_trace:+ if not isinstance(item, dict):+ continue+ tool_name = str(item.get("tool") or "unknown")[:256]+ status = str(item.get("status") or "unknown").lower()+ if status not in {"ok", "error"}:+ status = "unknown"++ def _byte_count(key: str) -> int:+ value = item.get(key, 0)+ if not isinstance(value, (int, float)) or isinstance(value, bool):+ return 0+ return max(0, int(value))++ history.append({+ "tool_name": tool_name,+ "tool_input": _sanitize_tool_input_summary(item.get("input_summary")),+ "input_bytes": _byte_count("args_bytes"),+ "output_bytes": _byte_count("result_bytes"),+ "status": status,+ })+ return history+++def _looks_like_error_output(content: Any) -> bool:+ """Conservative stderr/error detector for tool-result previews.++ The old heuristic flagged any preview containing the substring "error",+ which painted perfectly normal terminal/json output red. We now only+ mark output as an error when there is stronger evidence:+ - structured JSON with an ``error`` key+ - structured JSON with ``status`` of error/failed+ - first line starts with a classic error marker+ """+ content = _stringify_tool_content(content)+ if not content:+ return False++ head = content.lstrip()+ if head.startswith("{") or head.startswith("["):+ try:+ parsed = json.loads(content)+ if isinstance(parsed, dict):+ if parsed.get("error"):+ return True+ status = str(parsed.get("status") or "").strip().lower()+ if status in {"error", "failed", "failure", "timeout"}:+ return True+ except Exception:+ pass++ first = content.splitlines()[0].strip().lower() if content.splitlines() else ""+ return (+ first.startswith("error:")+ or first.startswith("failed:")+ or first.startswith("traceback ")+ or first.startswith("exception:")+ )+++# Hard per-summary character ceiling layered on top of the dynamic+# headroom budget (see _apply_summary_budget). Belt-and-suspenders for+# models that ignore the "be concise" instruction. 0 disables the ceiling.+DEFAULT_MAX_SUMMARY_CHARS = 24000++# Fraction of the parent's *remaining* context headroom that the whole batch+# of subagent summaries is allowed to consume. The per-summary budget is this+# slice divided across the batch, so N children can't collectively blow the+# parent's window (the compression/429 death-spiral in issue/PR #9126).+_SUMMARY_HEADROOM_FRACTION = 0.5++# Floor so a single summary always gets a usable slice even when the parent is+# already nearly full — below this we'd be truncating to noise.+_MIN_SUMMARY_CHARS = 2000+++def _spill_summary_to_file(task_index: int, summary: str) -> Optional[str]:+ """Write a subagent's full summary to the delegation cache and return path.++ Mirrors web_extract's ``_store_full_text``: the file lands in+ ``cache/delegation`` which is mounted read-only into remote backends+ (Docker/Modal/SSH) via ``credential_files._CACHE_DIRS``, so the parent's+ terminal/``read_file`` tools can page through the complete text on any+ backend. Returns the absolute path, or None on failure (best-effort:+ the trimmed head+tail is still returned to the parent regardless).+ """+ try:+ from hermes_constants import get_hermes_dir+ import datetime as _dt++ cache_dir = get_hermes_dir("cache/delegation", "delegation_cache")+ cache_dir.mkdir(parents=True, exist_ok=True)+ ts = _dt.datetime.now().strftime("%Y%m%d_%H%M%S_%f")+ path = cache_dir / f"subagent-summary-{task_index}-{ts}.txt"+ from tools.spill_safety import write_text_exclusive++ # Exclusive symlink-refusing create; not private because+ # cache/delegation is bind-mounted read-only into remote backends+ # whose container UID must be able to read it.+ write_text_exclusive(path, summary, private=False)+ return str(path)+ except Exception as exc:+ logger.debug("Failed to spill subagent summary to file: %s", exc)+ return None+++def _trim_summary_with_footer(+ summary: str, cap: int, task_index: int+) -> tuple[str, Optional[str]]:+ """Return (model_text, spill_path) for one over-budget summary.++ Mirrors web_extract's ``_truncate_with_footer``: keep a head+tail window+ (~75% head / ~25% tail, snapped to line boundaries) so the subagent's+ opening AND its closing (outcomes / files-changed / issues, which live at+ the end) both survive, spill the full text to disk, and append a footer+ telling the parent exactly how much it's seeing and the precise+ ``read_file offset=`` to page into the omitted middle. Deterministic.+ """+ original_len = len(summary)+ head_budget = int(cap * 0.75)+ tail_budget = cap - head_budget++ head = summary[:head_budget]+ tail = summary[-tail_budget:]+ # Snap the head cut back to the last newline so we don't slice mid-line.+ nl = head.rfind("\n")+ if nl > head_budget * 0.5:+ head = head[:nl]+ # Snap the tail cut forward to the next newline for the same reason.+ nl = tail.find("\n")+ if 0 <= nl < tail_budget * 0.5:+ tail = tail[nl + 1:]++ spill_path = _spill_summary_to_file(task_index, summary)++ footer_lines = [+ "",+ "─" * 8 + " [SUMMARY TRUNCATED] " + "─" * 8,+ f"Showing {len(head):,} chars (head) + {len(tail):,} chars (tail) "+ f"of {original_len:,} total — trimmed to protect the parent's context window.",+ ]+ if spill_path:+ # read_file is 1-indexed; +2 moves past the last head line shown.+ middle_start_line = head.count("\n") + 2+ footer_lines.append(f"Full subagent output saved to: {spill_path}")+ footer_lines.append(+ f'To read the omitted middle: read_file path="{spill_path}" '+ f"offset={middle_start_line} limit=200 (the file is the complete "+ f"summary; raise/lower offset to page through it)."+ )+ else:+ footer_lines.append(+ "Full output could not be stored to disk; the head+tail above is "+ "all that was preserved."+ )+ footer_lines.append("─" * 37)++ model_text = head + "\n\n[... middle omitted — see footer ...]\n\n" + tail + "\n".join(footer_lines)+ return model_text, spill_path+++def _parent_summary_char_budget(parent_agent, n_summaries: int) -> Optional[int]:+ """Per-summary character budget sized against the parent's *remaining*+ context headroom, split across the batch.++ The overflow this guards against is N summaries entering the parent+ context at once (batch fan-out), not any single summary being large. We+ take a fraction of the headroom the parent has left (resolved context+ length minus what's already in its prompt) and divide it across the batch,+ converting tokens→chars at the standard ~4 chars/token estimate.++ Returns the per-summary char budget, or None when the parent's context+ state is unknown (no compressor / no token count) — in which case the+ caller falls back to the static char ceiling only.+ """+ try:+ compressor = getattr(parent_agent, "context_compressor", None)+ context_length = getattr(compressor, "context_length", None)+ if not isinstance(context_length, int) or context_length <= 0:+ return None++ used_tokens = getattr(parent_agent, "session_prompt_tokens", 0)+ if not isinstance(used_tokens, (int, float)) or used_tokens < 0:+ used_tokens = 0++ # Reserve the compressor's output budget so we measure INPUT headroom.+ reserved = getattr(compressor, "max_tokens", 0) or 0+ headroom_tokens = context_length - int(used_tokens) - int(reserved)+ if headroom_tokens <= 0:+ # Parent is already over budget — give each summary only the floor.+ return _MIN_SUMMARY_CHARS++ batch_token_budget = int(headroom_tokens * _SUMMARY_HEADROOM_FRACTION)+ per_summary_tokens = batch_token_budget // max(1, n_summaries)+ per_summary_chars = per_summary_tokens * 4 # ~4 chars/token+ return max(_MIN_SUMMARY_CHARS, per_summary_chars)+ except Exception:+ logger.debug("Summary budget computation failed", exc_info=True)+ return None+++def _apply_summary_budget(results: List[Dict[str, Any]], parent_agent) -> None:+ """Trim subagent summaries in-place so the batch can't overflow the+ parent's context window, spilling full text to disk so nothing is lost.++ The effective per-summary cap is the MIN of:+ - the dynamic headroom budget (remaining parent context ÷ batch size), and+ - the static ``delegation.max_summary_chars`` ceiling (0 = disabled).++ When a summary exceeds the cap, its full text is written to a file and the+ in-context summary becomes a head slice plus a pointer to that file. This+ addresses issue/PR #9126: batch fan-out returned N full summaries verbatim,+ blowing the parent context and (on rate-limited providers) triggering a+ compression/429 death spiral.+ """+ from tools.delegate_tool import _load_config+ summaries = [+ r for r in results if isinstance(r, dict) and isinstance(r.get("summary"), str) and r["summary"]+ ]+ if not summaries:+ return++ cfg = _load_config()+ try:+ static_ceiling = int(cfg.get("max_summary_chars", DEFAULT_MAX_SUMMARY_CHARS))+ except (TypeError, ValueError):+ static_ceiling = DEFAULT_MAX_SUMMARY_CHARS++ dynamic_budget = _parent_summary_char_budget(parent_agent, len(summaries))++ # Combine the two caps. Either can be absent/disabled.+ candidates = [c for c in (static_ceiling, dynamic_budget) if c and c > 0]+ if not candidates:+ return # both disabled / unknown → leave summaries untouched+ cap = min(candidates)++ for entry in summaries:+ summary = entry["summary"]+ if len(summary) <= cap:+ continue+ original_len = len(summary)+ model_text, spill_path = _trim_summary_with_footer(+ summary, cap, entry.get("task_index", -1)+ )+ entry["summary"] = model_text+ entry["summary_truncated"] = True+ if spill_path:+ entry["summary_full_path"] = spill_path+ logger.debug(+ "[subagent-%s] summary trimmed %d → ~%d chars (spill=%s)",+ entry.get("task_index", "?"),+ original_len,+ cap,+ spill_path or "none",+ )+++_PARENT_FINALIZATION_LOCK_GUARD = threading.Lock()++_PARENT_FINALIZATION_FALLBACK_LOCK = threading.RLock()++_CHILD_CONSTRUCTION_LOCK = threading.RLock()+++def _build_child_preserving_parent_tools(**kwargs):+ """Build a child without leaking its resolved toolset into the parent."""+ from tools.delegate_tool import _build_child_agent+ import model_tools++ with _CHILD_CONSTRUCTION_LOCK:+ parent_tool_names = list(model_tools._last_resolved_tool_names)+ try:+ child = _build_child_agent(**kwargs)+ finally:+ model_tools._last_resolved_tool_names = parent_tool_names+ child._delegate_saved_tool_names = parent_tool_names+ return child+++def _parent_finalization_lock(parent_agent) -> threading.RLock:+ """Return the per-parent lock that serializes lifecycle side effects."""+ if parent_agent is None:+ return _PARENT_FINALIZATION_FALLBACK_LOCK+ lock = getattr(parent_agent, "_subagent_finalization_lock", None)+ if lock is not None:+ return lock+ with _PARENT_FINALIZATION_LOCK_GUARD:+ lock = getattr(parent_agent, "_subagent_finalization_lock", None)+ if lock is None:+ lock = threading.RLock()+ try:+ setattr(parent_agent, "_subagent_finalization_lock", lock)+ except Exception:+ return _PARENT_FINALIZATION_FALLBACK_LOCK+ return lock+++def _finalize_child_results(+ results: List[Dict[str, Any]],+ task_list: List[Dict[str, Any]],+ children: List[tuple[int, Dict[str, Any], Any]],+ parent_agent,+) -> None:+ """Apply host-owned summary, memory, hook, and cost contracts once."""+ with _parent_finalization_lock(parent_agent):+ _apply_summary_budget(results, parent_agent)+ child_by_index = {index: child for index, _task, child in children}++ if parent_agent and getattr(parent_agent, "_memory_manager", None):+ for entry in results:+ try:+ task_index = entry.get("task_index", -1)+ task_goal = (+ task_list[task_index]["goal"]+ if isinstance(task_index, int)+ and 0 <= task_index < len(task_list)+ else ""+ )+ child = child_by_index.get(task_index)+ parent_agent._memory_manager.on_delegation(+ task=task_goal,+ result=entry.get("summary", "") or "",+ child_session_id=getattr(child, "session_id", ""),+ )+ except Exception:+ pass++ parent_session_id = getattr(parent_agent, "session_id", None)+ try:+ from hermes_cli.plugins import invoke_hook as invoke_hook+ except Exception:+ invoke_hook = None++ children_cost_total = 0.0+ for entry in results:+ child_role = entry.pop("_child_role", None)+ child_cost = entry.pop("_child_cost_usd", 0.0)+ try:+ if child_cost:+ children_cost_total += float(child_cost)+ except (TypeError, ValueError):+ pass+ if invoke_hook is None:+ continue+ try:+ child_index = entry.get("task_index", -1)+ child = child_by_index.get(child_index)+ invoke_hook(+ "subagent_stop",+ parent_session_id=parent_session_id,+ parent_turn_id=getattr(parent_agent, "_current_turn_id", "") or "",+ child_session_id=getattr(child, "session_id", None),+ child_role=child_role,+ child_summary=entry.get("summary"),+ child_status=entry.get("status"),+ tool_call_history=_subagent_stop_tool_call_history(+ entry.get("tool_trace")+ ),+ duration_ms=int((entry.get("duration_seconds") or 0) * 1000),+ )+ except Exception:+ logger.debug("subagent_stop hook invocation failed", exc_info=True)++ if children_cost_total > 0.0:+ try:+ current = float(+ getattr(parent_agent, "session_estimated_cost_usd", 0.0) or 0.0+ )+ parent_agent.session_estimated_cost_usd = current + children_cost_total+ if getattr(parent_agent, "session_cost_source", "none") in {+ None,+ "",+ "none",+ }:+ parent_agent.session_cost_source = "subagent"+ if getattr(parent_agent, "session_cost_status", "unknown") in {+ None,+ "",+ "unknown",+ }:+ parent_agent.session_cost_status = "estimated"+ except Exception:+ logger.debug("Subagent cost rollup failed", exc_info=True)+++def _run_child_lifecycle(+ task_index: int,+ goal: str,+ child=None,+ parent_agent=None,+) -> Dict[str, Any]:+ """Run one child and apply the same host lifecycle used by delegate_task."""+ from tools.delegate_tool import _run_single_child+ result = _run_single_child(task_index, goal, child, parent_agent)+ result.setdefault("task_index", task_index)+ task = {"goal": goal}+ _finalize_child_results(+ [result],+ [{"goal": ""} for _ in range(task_index)] + [task],+ [(task_index, task, child)],+ parent_agent,+ )+ return resultdiff --git a/tools/delegation_live_log.py b/tools/delegation_live_log.pyindex 9e17d8c34ce..b8ecce33f4c 100644--- a/tools/delegation_live_log.py+++ b/tools/delegation_live_log.py@@ -1,19 +1,15 @@"""Live, tail-able transcripts for delegated subagents.-Every ``delegate_task`` dispatch creates one append-only, human-readable log-per child under ``<hermes_home>/cache/delegation/live/<delegation_id>/task-<n>.log``.-Files are pre-created with a header at dispatch (so ``tail -f`` attaches-immediately) and stream one line per child event; the paths are returned from-``delegate_task`` so the parent or user can watch a child work.-+Each ``delegate_task`` dispatch creates one append-only log per child under+``<hermes_home>/cache/delegation/live/<delegation_id>/task-<n>.log``, pre-created+with a header at dispatch (so ``tail -f`` attaches immediately), streaming one+line per child event; paths are returned from ``delegate_task``.``cache/delegation`` is mounted read-only into remote terminal backends-(``credential_files._CACHE_DIRS``), so the logs are readable from any backend —-and every line written here must therefore be credential-redacted.--Design constraints: never raise into the agent loop (first write failure-disables the writer); open in append mode per write (no handle to lose on a-child crash); side-channel only (prompt cache unaffected); no config knobs-(retention is a 7-day module constant pruned on each dispatch).+(``credential_files._CACHE_DIRS``), so every line written here must be+credential-redacted. Constraints: never raise into the agent loop (first write+failure disables the writer); append mode per write (no handle to lose on a+child crash, close() is the flush); side-channel only (prompt cache unaffected);+no config knobs (7-day retention constant, pruned on each dispatch)."""from __future__ import annotations@@ -68,12 +64,8 @@ def _one_line(text: Any, limit: int) -> str:def _redact(text: str) -> str:"""Mask credentials before anything reaches the sandbox-readable transcript.-- Tool args, results and streamed text are exactly the data that carries- secrets. ``force=True`` because this is a safety boundary (redact even- when the global toggle is off); if the redactor is unavailable, withhold- the line rather than write a live credential into a mounted file.- """+ ``force=True``: safety boundary, redact even when the global toggle is off;+ if the redactor is unavailable, withhold the line rather than leak."""if not text:return texttry:@@ -89,11 +81,9 @@ def _dump_json(path: Path, payload: Dict[str, Any]) -> None:class LiveTranscriptWriter:- """Append-only human-readable event log for ONE subagent task.-- Best-effort: the first write failure flips ``_ok`` off and later calls- become debug-logged no-ops. Never raises.- """+ """Append-only human-readable event log for ONE subagent task. Best-effort:+ the first write failure flips ``_ok`` off and later calls become+ debug-logged no-ops. Never raises."""def __init__(self, delegation_id: str, task_index: int, goal: str,context: Optional[str] = None, root: Optional[Path] = None):@@ -104,33 +94,30 @@ class LiveTranscriptWriter:self._stream_buf: List[str] = []self._stream_len = 0try:+ goal_line = _one_line(goal, _KICKOFF_MAX)d = (root if root is not None else live_transcript_root()) / delegation_idd.mkdir(parents=True, exist_ok=True)self.path: Optional[Path] = d / f"task-{task_index}.log"- header = [- "=== Hermes subagent live transcript ===",- f"delegation: {delegation_id} task: {task_index}",- # Header bypasses event(), so redact here too.- f"goal: {_redact(_one_line(goal, _KICKOFF_MAX))}",- f"started: {time.strftime(_TIME_FMT)}",- "(append-only; streams while the subagent runs — tail -f me)",- "=" * 40,- ]- self.path.write_text("\n".join(header) + "\n", encoding="utf-8")- self.event("user", "kickoff: " + _one_line(goal, _KICKOFF_MAX)+ header = (+ "=== Hermes subagent live transcript ===\n"+ f"delegation: {delegation_id} task: {task_index}\n"+ f"goal: {_redact(goal_line)}\n" # header bypasses event(), so redact here too+ f"started: {time.strftime(_TIME_FMT)}\n"+ "(append-only; streams while the subagent runs — tail -f me)\n"+ + "=" * 40 + "\n"+ )+ self.path.write_text(header, encoding="utf-8")+ self.event("user", "kickoff: " + goal_line+ (f" | context: {_one_line(context, _KICKOFF_MAX)}" if context else ""))except Exception as exc:- logger.debug("Live transcript init failed (%s task %s): %s",- delegation_id, task_index, exc)+ logger.debug("Live transcript init failed (%s task %s): %s", delegation_id, task_index, exc)self._ok = Falseself.path = Nonedef event(self, role: str, text: str) -> None:- """Append one ``HH:MM:SS role | text`` line. Flushed per event.-- Single choke point: every typed helper funnels through here, so one- redaction covers args, results, thinking and streamed text.- """+ """Append one ``HH:MM:SS role | text`` line, flushed per event. Single+ choke point: every typed helper funnels through here so one redaction+ covers args, results, thinking and streamed text."""if not self._ok or self.path is None:returnline = f"{time.strftime('%H:%M:%S')} {role:<9}| {_redact(text)}\n"@@ -141,16 +128,16 @@ class LiveTranscriptWriter:self._ok = Falselogger.debug("Live transcript write failed (%s): %s", self.path, exc)- # ── typed helpers ────────────────────────────────────────────────────- def assistant_text(self, text: str) -> None:- t = _one_line(text, _ASSISTANT_MAX)+ def _line(self, role: str, text: str, limit: int) -> None:+ t = _one_line(text, limit)if t:- self.event("assistant", t)+ self.event(role, t)++ def assistant_text(self, text: str) -> None:+ self._line("assistant", text, _ASSISTANT_MAX)def thinking(self, text: str) -> None:- t = _one_line(text, _THINKING_MAX)- if t:- self.event("think", t)+ self._line("think", text, _THINKING_MAX)def tool_start(self, name: str, args_preview: Any = None) -> None:self.flush_stream()@@ -159,21 +146,17 @@ class LiveTranscriptWriter:def tool_result(self, name: str, result: Any = None,duration: Any = None, is_error: bool = False) -> None:status = "ERROR" if is_error else "ok"- dur = ""try:- if duration is not None:- dur = f" {float(duration):.1f}s"+ dur = "" if duration is None else f" {float(duration):.1f}s"except (TypeError, ValueError):- pass- self.event("result", f"{name or '?'} {status}{dur}: "- f"{_one_line(result, _RESULT_MAX)}")+ dur = ""+ self.event("result", f"{name or '?'} {status}{dur}: {_one_line(result, _RESULT_MAX)}")def marker(self, text: str) -> None:"""Lifecycle marker: start / final / error / interrupt / budget."""self.flush_stream()self.event("final", _one_line(text, _ASSISTANT_MAX))- # ── streamed reply buffering ─────────────────────────────────────────def add_stream_delta(self, delta: str) -> None:"""Buffer streamed assistant reply text; flushed as one line."""if not delta or not self._ok:@@ -184,92 +167,61 @@ class LiveTranscriptWriter:self.flush_stream()def flush_stream(self) -> None:- if not self._stream_buf:- return- text = "".join(self._stream_buf)- self._stream_buf = []- self._stream_len = 0- self.assistant_text(text)-- # ── event demux (the tool_progress_callback surface) ─────────────────- def _on_tool_started(self, tool_name, preview, args, kwargs):- self.tool_start(str(tool_name or ""), preview if preview else args)+ if self._stream_buf:+ text, self._stream_buf, self._stream_len = "".join(self._stream_buf), [], 0+ self.assistant_text(text)def _on_tool_completed(self, tool_name, preview, args, kwargs):- self.tool_result(- str(tool_name or ""),- result=kwargs.get("result"),- duration=kwargs.get("duration"),- is_error=bool(kwargs.get("is_error")),- )-- def _on_thinking(self, tool_name, preview, args, kwargs):- # Fired as cb("_thinking", <text>) — text rides in the tool_name slot.- self.thinking(str(tool_name or preview or ""))-- def _on_reasoning(self, tool_name, preview, args, kwargs):- # cb("reasoning.available", "_thinking", <text>, None)- self.thinking(str(preview or ""))-- def _on_text(self, tool_name, preview, args, kwargs):- self.add_stream_delta(str(preview or ""))-- def _on_start(self, tool_name, preview, args, kwargs):- self.event("start", _one_line(preview, _KICKOFF_MAX))+ self.tool_result(str(tool_name or ""), result=kwargs.get("result"),+ duration=kwargs.get("duration"), is_error=bool(kwargs.get("is_error")))def _on_complete(self, tool_name, preview, args, kwargs):self.flush_stream()- parts = [f"status={kwargs.get('status', '?')}"]dur = kwargs.get("duration_seconds")- if dur is not None:- parts.append(f"duration={dur}s")summary = kwargs.get("summary") or preview- if summary:- parts.append(f"summary: {_one_line(summary, _RESULT_MAX)}")- self.marker(" ".join(parts))+ self.marker(" ".join(filter(None, [+ f"status={kwargs.get('status', '?')}",+ f"duration={dur}s" if dur is not None else "",+ f"summary: {_one_line(summary, _RESULT_MAX)}" if summary else "",+ ])))+ # Event demux (the tool_progress_callback surface): handler(self, tool_name, preview, args, kwargs)._OBSERVERS = {- "tool.started": _on_tool_started,+ "tool.started": lambda s, n, p, a, kw: s.tool_start(str(n or ""), p if p else a),"tool.completed": _on_tool_completed,- "_thinking": _on_thinking,- "reasoning.available": _on_reasoning,- "subagent.text": _on_text,- "subagent.start": _on_start,+ # Fired as cb("_thinking", <text>) — text rides in the tool_name slot.+ "_thinking": lambda s, n, p, a, kw: s.thinking(str(n or p or "")),+ # cb("reasoning.available", "_thinking", <text>, None)+ "reasoning.available": lambda s, n, p, a, kw: s.thinking(str(p or "")),+ "subagent.text": lambda s, n, p, a, kw: s.add_stream_delta(str(p or "")),+ "subagent.start": lambda s, n, p, a, kw: s.event("start", _one_line(p, _KICKOFF_MAX)),"subagent.complete": _on_complete,}def observe(self, event_type: Any, tool_name: Any = None,preview: Any = None, args: Any = None, **kwargs: Any) -> None:- """Map a child tool_progress_callback event onto transcript lines.-- Mirrors the shapes emitted by agent/tool_executor.py,- agent/conversation_loop.py and delegate_tool._run_single_child.- Unknown events are ignored. Never raises (event() swallows I/O).- """+ """Map a child tool_progress_callback event (shapes from agent/tool_executor.py,+ agent/conversation_loop.py, delegate_tool._run_single_child) onto transcript+ lines. Unknown events are ignored. Never raises (event() swallows I/O)."""handler = self._OBSERVERS.get(str(event_type or ""))if handler is not None:handler(self, tool_name, preview, args, kwargs)def finalize(self, entry: Dict[str, Any]) -> None:"""Terminal marker with exit-reason detail subagent.complete lacks."""- parts = [f"end status={entry.get('status', '?')}"]exit_reason = entry.get("exit_reason")- if exit_reason:- parts.append(f"exit_reason={exit_reason}")- if exit_reason == "max_iterations":- parts.append("(iteration budget exhausted)")- if entry.get("error"):- parts.append(f"error: {_one_line(entry['error'], _RESULT_MAX)}")- self.marker(" ".join(parts))+ self.marker(" ".join(filter(None, [+ f"end status={entry.get('status', '?')}",+ f"exit_reason={exit_reason}" if exit_reason else "",+ "(iteration budget exhausted)" if exit_reason == "max_iterations" else "",+ f"error: {_one_line(entry['error'], _RESULT_MAX)}" if entry.get("error") else "",+ ])))def wrap_progress_callback(inner_cb, writer: LiveTranscriptWriter):"""Wrap a child's tool_progress_callback so events also land in the log.-``inner_cb`` may be None; writer failures never propagate and the inner- callback's behavior is unchanged. Preserves the ``_flush`` attribute- contract used by _run_single_child.- """+ callback is unchanged. Preserves the ``_flush`` attribute contract."""def _cb(event_type, tool_name=None, preview=None, args=None, **kwargs):try:@@ -284,16 +236,13 @@ def wrap_progress_callback(inner_cb, writer: LiveTranscriptWriter):writer.flush_stream()except Exception:pass- inner_flush = getattr(inner_cb, "_flush", None)- if callable(inner_flush):- inner_flush()+ if callable(getattr(inner_cb, "_flush", None)):+ inner_cb._flush()_cb._flush = _flushreturn _cb-# ── dispatch-time helpers ────────────────────────────────────────────────-def create_live_transcripts(task_list: List[Dict[str, Any]],context: Optional[str] = None,@@ -301,12 +250,9 @@ def create_live_transcripts(model: Optional[str] = None,provider: Optional[str] = None,) -> tuple[Optional[str], List[Optional[LiveTranscriptWriter]], List[str]]:- """Create one pre-headered writer per task + a manifest.json.-- Returns ``(delegation_id, writers, paths)``; on any top-level failure- ``(None, [None]*n, [])`` so delegation proceeds untouched. Also prunes- stale live dirs opportunistically.- """+ """Create one pre-headered writer per task + a manifest.json; also prunes+ stale live dirs. Returns ``(delegation_id, writers, paths)``; on any+ top-level failure ``(None, [None]*n, [])`` so delegation proceeds untouched."""n = len(task_list)try:prune_stale_live_dirs()@@ -314,16 +260,10 @@ def create_live_transcripts(passtry:deleg_id = delegation_id or new_live_delegation_id()- writers: List[Optional[LiveTranscriptWriter]] = []- paths: List[str] = []- for i, t in enumerate(task_list):- w = LiveTranscriptWriter(- deleg_id, i, str(t.get("goal", "")),- context=t.get("context") or context,- )- writers.append(w if w.path is not None else None)- if w.path is not None:- paths.append(str(w.path))+ made = [LiveTranscriptWriter(deleg_id, i, str(t.get("goal", "")), context=t.get("context") or context)+ for i, t in enumerate(task_list)]+ writers: List[Optional[LiveTranscriptWriter]] = [w if w.path is not None else None for w in made]+ paths: List[str] = [str(w.path) for w in made if w.path is not None]if not paths:return None, [None] * n, []_write_manifest(deleg_id, task_list, paths, model=model, provider=provider)@@ -341,25 +281,20 @@ def _write_manifest(delegation_id: str, task_list: List[Dict[str, Any]],paths: List[str], model: Optional[str] = None,provider: Optional[str] = None) -> None:try:- manifest = {+ _dump_json(_manifest_path(delegation_id), {"delegation_id": delegation_id,"started": time.strftime(_TIME_FMT),"task_count": len(task_list),"model": model,"provider": provider,- "tasks": [- {- "index": i,- # Same mounted directory as the .log files, so the goal- # needs the same redaction here.- "goal": _redact(str(t.get("goal", ""))[:500]),- "log": paths[i] if i < len(paths) else None,- "status": "running",- }- for i, t in enumerate(task_list)- ],- }- _dump_json(_manifest_path(delegation_id), manifest)+ "tasks": [{+ "index": i,+ # Same mounted dir as the .log files, so the goal needs the same redaction.+ "goal": _redact(str(t.get("goal", ""))[:500]),+ "log": paths[i] if i < len(paths) else None,+ "status": "running",+ } for i, t in enumerate(task_list)],+ })except Exception as exc:logger.debug("Live transcript manifest write failed: %s", exc)diff --git a/tools/delegation_output_schema.py b/tools/delegation_output_schema.pyindex e30a3c674a2..1f8d0abcdaa 100644--- a/tools/delegation_output_schema.py+++ b/tools/delegation_output_schema.py@@ -1,14 +1,10 @@-"""Structured-output schema helpers for delegate_task (T1-24).+"""Structured-output schema helpers for delegate_task.-Optional per-task ``output_schema`` (a JSON Schema object): the child is-told about the contract via an OUTPUT CONTRACT block appended to its-context, the parent validates the child's final answer with jsonschema,-and on failure sends exactly ONE bounded retry turn carrying the-validation errors verbatim (per llm-structured-output-schema-design:-max 1 retry, exact errors, no schema re-paste).--Pattern from: github/copilot-cli ctx.agent(prompt, {schema}) — PATTERN-ONLY, zero code/prompt text copied (proprietary).+Optional per-task ``output_schema`` (a JSON Schema object): the child gets an+OUTPUT CONTRACT block appended to its context, the parent validates the final+answer with jsonschema, and on failure sends exactly ONE bounded retry turn+carrying the validation errors verbatim (max 1 retry — more retries make+frontier models drop fields that were right the first time; no schema re-paste)."""from __future__ import annotations@@ -19,17 +15,13 @@ from typing import Any, Dict, List, Optional, Tuplelogger = logging.getLogger(__name__)-# Exactly one retry turn — bounded by design. More retries make frontier-# models drop fields that were right the first time.-MAX_SCHEMA_RETRIES = 1-_CONTRACT_HEADER = "OUTPUT CONTRACT (machine-validated)"def coerce_output_schema(raw: Any) -> Tuple[Optional[Dict[str, Any]], Optional[str]]:"""Validate a model/caller-supplied output_schema value.- Returns ``(schema, None)`` when usable, ``(None, error)`` when not.+ Returns ``(schema, None)`` when usable, ``(None, error)`` when not;``None`` input passes through as ``(None, None)`` (no schema requested)."""if raw is None:@@ -44,16 +36,13 @@ def coerce_output_schema(raw: Any) -> Tuple[Optional[Dict[str, Any]], Optional[sreturn None, "output_schema must be a JSON Schema object."raw = parsedif not isinstance(raw, dict):- return None, (- f"output_schema must be a JSON Schema object, got {type(raw).__name__}."- )+ return None, f"output_schema must be a JSON Schema object, got {type(raw).__name__}."try:from jsonschema.validators import validator_for # type: ignore[import-untyped]validator_for(raw).check_schema(raw)except ImportError:- # jsonschema is a hard dependency in practice; degrade to accepting- # the dict as-is so delegation still works without it.+ # Degrade to accepting the dict as-is so delegation still works without jsonschema.logger.debug("jsonschema unavailable; skipping output_schema meta-validation")except Exception as exc:return None, f"output_schema is not a valid JSON Schema: {exc}"@@ -78,12 +67,9 @@ def append_output_contract(context: Optional[str], schema: Dict[str, Any]) -> stdef extract_json_candidate(text: str) -> str:- """Best-effort extraction of a JSON payload from model output.-- Strips markdown code fences and leading/trailing prose around the- outermost ``{...}`` / ``[...]`` span. Returns the (possibly unchanged)- candidate string; parsing errors are reported by validate_output.- """+ """Strip markdown fences and prose around the outermost ``{...}``/``[...]``+ span. Returns the (possibly unchanged) candidate; parse errors are+ reported by validate_output."""raw = (text or "").strip()if raw.startswith("```"):raw = raw.split("\n", 1)[-1]@@ -102,13 +88,11 @@ def extract_json_candidate(text: str) -> str:return raw-def validate_output(- text: str, schema: Dict[str, Any]-) -> Tuple[bool, List[str]]:+def validate_output(text: str, schema: Dict[str, Any]) -> Tuple[bool, List[str]]:"""Validate a child's final answer against ``schema``.- Returns ``(True, [])`` on success or ``(False, errors)`` where errors- are human-readable strings suitable for the retry turn.+ Returns ``(True, [])`` or ``(False, errors)`` with human-readable strings+ suitable for the retry turn."""candidate = extract_json_candidate(text or "")if not candidate.strip():@@ -124,23 +108,16 @@ def validate_output(return True, []validator = validator_for(schema)(schema)errors = sorted(validator.iter_errors(parsed), key=lambda e: list(e.absolute_path))- if not errors:- return True, []- rendered: List[str] = []- for err in errors[:10]: # bound error volume for the retry prompt- path = "$" + "".join(- f"[{p}]" if isinstance(p, int) else f".{p}" for p in err.absolute_path- )- rendered.append(f"{path}: {err.message}")- return False, rendered+ rendered = [ # bound error volume for the retry prompt+ "$" + "".join(f"[{p}]" if isinstance(p, int) else f".{p}" for p in err.absolute_path) + f": {err.message}"+ for err in errors[:10]+ ]+ return not rendered, rendereddef build_retry_message(errors: List[str]) -> str:- """Build the single bounded retry turn sent to the child.-- Carries the validation errors verbatim; deliberately does NOT- re-paste the schema (the child already has it in its context).- """+ """Single bounded retry turn: errors verbatim, deliberately NOT re-pasting+ the schema (the child already has it in its context)."""error_block = "\n".join(f"- {e}" for e in errors)return ("Your previous final response was rejected by the output contract "diff --git a/tools/subagent_worktree.py b/tools/subagent_worktree.pyindex 54611295f0f..ae987598391 100644--- a/tools/subagent_worktree.py+++ b/tools/subagent_worktree.py@@ -1,38 +1,18 @@-"""Opt-in git worktree isolation for delegated subagents.--Inspired by Muse Code's ``--subagent-worktree-isolation`` (Meta, Aug 2026):-when isolation is on, each delegated child agent gets its own git worktree-checked out from the parent's current commit, so parallel children never-contend for the same working copy and the parent's checkout stays untouched.-This is a clean-room implementation of the documented behavior-(https://dev.meta.ai/docs/muse-code/extending#multi-agent); no Muse Code-code was referenced.--Enable in config.yaml::-- delegation:- worktree_isolation: true # default: false--Contract (mirrors Muse Code's documented semantics):--- **Opt-in and git-only.** In a non-git workspace the setting is ignored- without an error and children share the parent's working directory,- exactly as before.-- **One worktree per child**, branched from the parent repo's current- ``HEAD`` under ``<repo>/.worktrees/subagent-<id>`` on branch- ``hermes-subagent/<id>``.-- **The parent reviews/merges.** Children commit inside their own worktree;- each result entry reports the worktree path, branch, commit count, and- dirty state so the parent can review or merge each branch.-- **Clean worktrees are pruned.** A worktree with no new commits and a- clean tree is removed automatically after the child finishes; anything- holding work is kept and reported. Pruning requires affirmative proof:- if a git inspection probe fails the state is unknown, so the worktree is- kept and the result entry carries ``inspection_failed`` + ``note`` (#88113).--Only the local terminal backend is supported: on docker/ssh/modal/etc. the-worktree created on the host would not be visible inside the sandbox, so-isolation is skipped (with a debug log) rather than half-applied.+"""Opt-in git worktree isolation for delegated subagents (clean-room port of+Muse Code's documented ``--subagent-worktree-isolation`` semantics).+Enable with ``delegation.worktree_isolation: true`` (default false).++Contract: git-only — in a non-git workspace the setting is ignored without+error and children share the parent's cwd. One worktree per child, branched+from the parent's ``HEAD`` under ``<repo>/.worktrees/subagent-<id>`` on branch+``hermes-subagent/<id>``. The parent reviews/merges: each result entry reports+path, branch, commit count and dirty state. A worktree is pruned only on+affirmative proof (zero commits AND clean tree, both probes succeeded); if a+probe fails the state is unknown, so it is kept and the entry carries+``inspection_failed`` + ``note``.++Local terminal backend only: on docker/ssh/modal the host worktree is invisible+inside the sandbox, so isolation is skipped (debug log) rather than half-applied."""from __future__ import annotations@@ -62,17 +42,9 @@ def _run_git(args, cwd: str, timeout: int = _GIT_TIMEOUT):fsmonitor/hooks/pager/credential config sinks keeps a malicious ``.git/config``from executing on the host."""- return subprocess.run(- ["git", *harden_git_argv(args)],- cwd=cwd,- capture_output=True,- text=True,- encoding="utf-8",- errors="replace",- timeout=timeout,- stdin=subprocess.DEVNULL,- env=noninteractive_git_env(),- )+ return subprocess.run(["git", *harden_git_argv(args)], cwd=cwd, capture_output=True,+ text=True, encoding="utf-8", errors="replace", timeout=timeout,+ stdin=subprocess.DEVNULL, env=noninteractive_git_env())def local_backend_active() -> bool:@@ -80,8 +52,7 @@ def local_backend_active() -> bool:try:from hermes_cli.config import load_config_readonly- cfg = load_config_readonly()- backend = ((cfg.get("terminal") or {}).get("backend") or "local")+ backend = (load_config_readonly().get("terminal") or {}).get("backend") or "local"return str(backend).strip().lower() in ("", "local")except Exception:# Legacy entry points without the shared loader default to local.@@ -94,19 +65,13 @@ def resolve_repo_root(path: Optional[str]) -> Optional[str]:return Nonetry:candidate = os.path.abspath(os.path.expanduser(str(path)))- except Exception:- return None- if not os.path.isdir(candidate):- return None- try:+ if not os.path.isdir(candidate):+ return Noneresult = _run_git(["rev-parse", "--show-toplevel"], cwd=candidate)except Exception as exc:logger.debug("subagent worktree: rev-parse failed: %s", exc)return None- if result.returncode != 0:- return None- root = result.stdout.strip()- return root or None+ return (result.stdout.strip() or None) if result.returncode == 0 else Nonedef _ensure_gitignore_entry(repo_root: str) -> None:@@ -114,11 +79,7 @@ def _ensure_gitignore_entry(repo_root: str) -> None:gitignore = Path(repo_root) / ".gitignore"entry = f"{_WORKTREES_DIRNAME}/"try:- existing = (- gitignore.read_text(encoding="utf-8-sig", errors="replace")- if gitignore.exists()- else ""- )+ existing = gitignore.read_text(encoding="utf-8-sig", errors="replace") if gitignore.exists() else ""if entry not in existing.splitlines():with open(gitignore, "a", encoding="utf-8") as f:if existing and not existing.endswith("\n"):@@ -128,23 +89,15 @@ def _ensure_gitignore_entry(repo_root: str) -> None:logger.debug("subagent worktree: could not update .gitignore: %s", exc)-def create_subagent_worktree(- parent_cwd: Optional[str],- subagent_id: Optional[str] = None,-) -> Optional[Dict[str, str]]:- """Create an isolated worktree for one child agent.-- Returns metadata (``path``, ``branch``, ``repo_root``, ``base_commit``)- on success, or ``None`` when the workspace is not a git repository or- worktree creation fails — mirroring Muse Code, absence of git downgrades- silently to shared-workspace behavior.- """+def create_subagent_worktree(parent_cwd: Optional[str], subagent_id: Optional[str] = None) -> Optional[Dict[str, str]]:+ """Create an isolated worktree for one child agent. Returns+ ``path``/``branch``/``repo_root``/``base_commit``, or ``None`` when not a git+ repo or creation fails — absence of git downgrades silently to shared workspace."""repo_root = resolve_repo_root(parent_cwd)if not repo_root:return None- short_id = (subagent_id or uuid.uuid4().hex[:8]).replace("/", "-")- wt_name = f"subagent-{short_id}"+ wt_name = f"subagent-{(subagent_id or uuid.uuid4().hex[:8]).replace('/', '-')}"branch = f"{_BRANCH_NAMESPACE}/{wt_name}"wt_path = Path(repo_root) / _WORKTREES_DIRNAME / wt_name@@ -159,48 +112,38 @@ def create_subagent_worktree(try:base = _run_git(["rev-parse", "HEAD"], cwd=repo_root)base_commit = base.stdout.strip() if base.returncode == 0 else ""- result = _run_git(- ["worktree", "add", str(wt_path), "-b", branch, "HEAD"],- cwd=repo_root,- )+ result = _run_git(["worktree", "add", str(wt_path), "-b", branch, "HEAD"], cwd=repo_root)except Exception as exc:logger.warning("subagent worktree: creation failed: %s", exc)return Noneif result.returncode != 0:# Common on repos with zero commits (unborn HEAD) — degrade silently.- logger.warning(- "subagent worktree: git worktree add failed: %s",- result.stderr.strip(),- )+ logger.warning("subagent worktree: git worktree add failed: %s", result.stderr.strip())return Nonelogger.info("subagent worktree created: %s (branch %s)", wt_path, branch)- return {- "path": str(wt_path),- "branch": branch,- "repo_root": repo_root,- "base_commit": base_commit,- }+ return {"path": str(wt_path), "branch": branch, "repo_root": repo_root, "base_commit": base_commit}+++def _base_payload(info: Dict[str, str]) -> Dict[str, Any]:+ """Result-entry schema the parent expects (no creation-side internals)."""+ return {"path": info.get("path", ""), "branch": info.get("branch", ""),+ "commits": 0, "dirty": False, "pruned": False}def mark_worktree_payload_unproven(payload: Dict[str, Any], reason: str, *, unmeasured: str = "commits/dirty") -> Dict[str, Any]:- """Flag a worktree result payload as un-inspected, in place (#88113).-- A failed probe proves nothing about the tree, so the fields it would have- filled keep their defaults. The parent agent only ever sees this dict — it- cannot read logs — so the uncertainty has to travel *in the payload*, or- "0 commits, clean" reads as "the child produced nothing" and the work we- just preserved is never looked at.-- *unmeasured* names only the fields this failure actually left unproven: one- probe can succeed while the other fails (a bad ``base_commit`` fails- ``rev-list`` while ``status`` still reports a real ``dirty``), and claiming- a measured value is UNKNOWN would be its own kind of misreport.-- Shared by ``finalize_subagent_worktree`` and ``delegate_tool``'s- finalize-raised fallback so the two producers of this schema cannot drift.+ """Flag a worktree result payload as un-inspected, in place.++ A failed probe proves nothing, so the fields it would have filled keep+ their defaults. The parent only sees this dict (not logs), so the+ uncertainty must travel in the payload or "0 commits, clean" reads as "the+ child produced nothing". *unmeasured* names only the fields actually left+ unproven: one probe can succeed while the other fails, and calling a+ measured value UNKNOWN would be its own misreport. Shared by+ ``finalize_subagent_worktree`` and ``delegate_tool``'s finalize-raised+ fallback so the two producers of this schema cannot drift."""path = payload.get("path", "")branch = payload.get("branch", "")@@ -210,138 +153,74 @@ def mark_worktree_payload_unproven("proven zero/clean. The worktree and branch were preserved "f"— inspect {path} (branch {branch}) before assuming no work.")- logger.warning(- "subagent worktree: git inspection failed (%s) — keeping %s "- "(branch %s) for manual review",- reason,- path,- branch,- )+ logger.warning("subagent worktree: git inspection failed (%s) — keeping %s (branch %s) for manual review",+ reason, path, branch)return payload-def unproven_worktree_payload(- info: Dict[str, str], reason: str-) -> Dict[str, Any]:- """Build a complete un-inspected payload from creation-side *info*.-- For callers that never got a payload back at all (``delegate_tool``'s- fallback when ``finalize_subagent_worktree`` itself raises). Emits exactly- the schema the parent expects — notably WITHOUT the creation-side- ``repo_root``/``base_commit`` internals.- """- return mark_worktree_payload_unproven(- {- "path": info.get("path", ""),- "branch": info.get("branch", ""),- "commits": 0,- "dirty": False,- "pruned": False,- },- reason,- )+def unproven_worktree_payload(info: Dict[str, str], reason: str) -> Dict[str, Any]:+ """Complete un-inspected payload for callers that never got one back+ (``delegate_tool``'s fallback when ``finalize_subagent_worktree`` raises).+ Emits exactly the parent-facing schema, WITHOUT ``repo_root``/``base_commit``."""+ return mark_worktree_payload_unproven(_base_payload(info), reason)-def finalize_subagent_worktree(- info: Dict[str, str], *, prune: bool = True-) -> Dict[str, Any]:+def finalize_subagent_worktree(info: Dict[str, str], *, prune: bool = True) -> Dict[str, Any]:"""Inspect (and possibly prune) a child worktree after the child finishes.-- Returns a result-entry payload: path, branch, ``commits`` ahead of the- base, ``dirty`` (uncommitted changes present), and ``pruned``. A worktree- with zero commits and a clean tree is removed when *prune* is true **and- both git probes succeeded**; anything holding work is always kept for the- parent to review or merge.-- If ``git rev-list``/``git status`` exits non-zero (or the inspection- raises), the tree state is unknown, so the worktree and branch are kept- and the payload carries ``inspection_failed: True`` plus a ``note``.- ``commits``/``dirty`` are then defaults, NOT measurements — the parent- must inspect the worktree instead of concluding the child did no work.- """+ Returns path, branch, ``commits`` ahead of base, ``dirty``, ``pruned``. Prunes+ only when *prune*, commits==0, clean tree AND both git probes succeeded. If a+ probe exits non-zero or raises, the worktree/branch are kept and the payload+ carries ``inspection_failed: True`` + ``note``; ``commits``/``dirty`` are then+ defaults, NOT measurements."""path = info.get("path", "")branch = info.get("branch", "")- repo_root = info.get("repo_root", "")base_commit = info.get("base_commit", "")-- payload: Dict[str, Any] = {- "path": path,- "branch": branch,- "commits": 0,- "dirty": False,- "pruned": False,- }+ payload = _base_payload(info)if not path or not os.path.isdir(path):payload["pruned"] = True # nothing on disk to reviewreturn payload- def _unproven(- reason: str, *, unmeasured: str = "commits/dirty"- ) -> Dict[str, Any]:- return mark_worktree_payload_unproven(- payload, reason, unmeasured=unmeasured- )-- # A worktree whose commit count was never measured must not be pruned- # either: the prune condition reads payload["commits"], and without a base- # commit that value is an unproven default, exactly the class of bug- # #88113 is about.+ # Without a base commit the count is an unproven default, and the prune+ # condition reads payload["commits"] — so it must not prune either.if not base_commit:- return _unproven(- "no base_commit recorded — commit count unmeasurable",- unmeasured="commits",+ return mark_worktree_payload_unproven(+ payload, "no base_commit recorded — commit count unmeasurable", unmeasured="commits")failed: list = []unmeasured: list = []try:- counted = _run_git(- ["rev-list", "--count", f"{base_commit}..HEAD"], cwd=path- )+ counted = _run_git(["rev-list", "--count", f"{base_commit}..HEAD"], cwd=path)if counted.returncode == 0:payload["commits"] = int(counted.stdout.strip() or 0)else:- failed.append(- f"rev-list exit {counted.returncode}: "- f"{counted.stderr.strip()[:200]}"- )+ failed.append(f"rev-list exit {counted.returncode}: {counted.stderr.strip()[:200]}")unmeasured.append("commits")status = _run_git(["status", "--porcelain"], cwd=path)if status.returncode == 0:payload["dirty"] = bool(status.stdout.strip())else:- failed.append(- f"status exit {status.returncode}: "- f"{status.stderr.strip()[:200]}"- )+ failed.append(f"status exit {status.returncode}: {status.stderr.strip()[:200]}")unmeasured.append("dirty")except Exception as exc:- # Same unknown state as a non-zero exit (timeout, OSError, or a- # non-numeric rev-list stdout) — keep the worktree rather than risk- # deleting work, and tell the caller the numbers are unproven. Which- # probe raised is unknowable here, so neither value is trustworthy.- return _unproven(f"inspection raised: {exc}")+ # Timeout, OSError or non-numeric rev-list stdout: which probe raised is+ # unknowable, so neither value is trustworthy — keep the worktree.+ return mark_worktree_payload_unproven(payload, f"inspection raised: {exc}")if failed:- # Fail-safe (#88113): a destructive cleanup requires affirmative proof- # of "zero commits + clean tree"; the defaults prove nothing.- return _unproven(- "; ".join(failed), unmeasured="/".join(unmeasured)- )+ # Destructive cleanup requires affirmative proof; defaults prove nothing.+ return mark_worktree_payload_unproven(payload, "; ".join(failed), unmeasured="/".join(unmeasured))if prune and payload["commits"] == 0 and not payload["dirty"]:+ cwd = info.get("repo_root", "") or pathtry:- removed = _run_git(- ["worktree", "remove", "--force", path], cwd=repo_root or path- )+ removed = _run_git(["worktree", "remove", "--force", path], cwd=cwd)if removed.returncode == 0:- _run_git(["branch", "-D", branch], cwd=repo_root or path)+ _run_git(["branch", "-D", branch], cwd=cwd)payload["pruned"] = Truelogger.info("subagent worktree pruned (no work): %s", path)else:- logger.debug(- "subagent worktree: prune failed: %s", removed.stderr.strip()- )+ logger.debug("subagent worktree: prune failed: %s", removed.stderr.strip())except Exception as exc:logger.debug("subagent worktree: prune failed: %s", exc)