The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(delegate): results module — one input-summary builder, tighter output-tail/trim helpers
Teknium52fdc02dfe37
diff --git a/tools/delegate_tool_results.py b/tools/delegate_tool_results.pyindex 7807f173587..35c67530ac9 100644--- a/tools/delegate_tool_results.py+++ b/tools/delegate_tool_results.py@@ -14,64 +14,9 @@ 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.- """+ """Stable text for tool-result content. Some OpenAI-compatible paths return+ content-block lists; observability must never crash on them."""if content is None:return ""if isinstance(content, str):@@ -87,23 +32,57 @@ def _stringify_tool_content(content: Any) -> str:return json.dumps(content, ensure_ascii=False, default=str)return str(content)+def _looks_like_error_output(content: Any) -> bool:+ """Conservative error detector for tool-result previews: structured JSON with+ an ``error`` key or an error/failed ``status``, or a first line starting with a+ classic error marker. (Substring "error" alone painted normal output red.)"""+ 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+ if str(parsed.get("status") or "").strip().lower() in {"error", "failed", "failure", "timeout"}:+ return True+ except Exception:+ pass+ first = content.splitlines()[0].strip().lower() if content.splitlines() else ""+ return first.startswith(("error:", "failed:", "traceback ", "exception:"))++def _extract_output_tail(result: Dict[str, Any], *, max_entries: int = 12, max_chars: int = 8000) -> List[Dict[str, Any]]:+ """Last N tool-call results ``{tool, preview, is_error}`` from a child's+ conversation (the overlay's "Output" section), chronological order. Content+ blocks are flattened first so a block-wrapped "Error: ..." is still flagged;+ line structure is preserved (capped at ``max_chars``) so the overlay shows+ real output rather than a whitespace-collapsed blob."""+ messages = result.get("messages") if isinstance(result, dict) else None+ if not isinstance(messages, list):+ return []+ name_by_call_id = {+ tc["id"]: str((tc.get("function") or {}).get("name") or "tool")+ for msg in messages if isinstance(msg, dict) and msg.get("role") == "assistant"+ for tc in msg.get("tool_calls") or [] if tc.get("id")+ }+ tail: List[Dict[str, Any]] = []+ for msg in reversed(messages): # newest first, then restore order below+ if len(tail) >= max_entries:+ break+ if not isinstance(msg, dict) or msg.get("role") != "tool":+ continue+ content = _stringify_tool_content(msg.get("content") or "")+ tool_name = name_by_call_id.get(msg.get("tool_call_id") or "", "tool")+ tail.append({"tool": tool_name, "preview": content[:max_chars], "is_error": _looks_like_error_output(content)})+ tail.reverse()+ return tail+_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",+ "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:@@ -126,16 +105,12 @@ def _sanitize_tool_target(key: str, value: Any) -> Any:# 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+ netloc = f"{host}:{parsed.port}" if parsed.port is not None else hostreturn urlunsplit((parsed.scheme, netloc, parsed.path, "", ""))except ValueError:return Nonereturn 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] = {}@@ -147,124 +122,77 @@ def _sanitize_targets(mapping: Dict[str, Any]) -> Dict[str, Any]:targets[key] = cleanedreturn targets+def _input_summary(keys: Any, targets: Any) -> Dict[str, Any]:+ """``{argument_keys, targets}`` with bounded, sanitized contents (empty on bad shapes)."""+ return {+ "argument_keys": [str(key)[:128] for key in keys[:64]] if isinstance(keys, list) else [],+ "targets": _sanitize_targets(targets) if isinstance(targets, dict) else {},+ }+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()+ parsed = None+ if isinstance(arguments, str):+ try:+ parsed = json.loads(arguments)+ except (TypeError, ValueError):+ passif 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}+ return _input_summary([], {})+ return _input_summary(sorted(str(key)[:128] for key in parsed), parsed)def _subagent_stop_tool_call_history(tool_trace: Any) -> List[Dict[str, Any]]:- """Build a detached, metadata-only tool history for lifecycle hooks."""+ """Detached, metadata-only tool history for lifecycle hooks (input summaries re-sanitized)."""if not isinstance(tool_trace, list):return []+ def _byte_count(item, key: str) -> int:+ value = item.get(key, 0)+ return max(0, int(value)) if isinstance(value, (int, float)) and not isinstance(value, bool) else 0+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))-+ summary = item.get("input_summary")+ summary = summary if isinstance(summary, dict) else {}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,+ "tool_name": str(item.get("tool") or "unknown")[:256],+ "tool_input": _input_summary(summary.get("argument_keys"), summary.get("targets")),+ "input_bytes": _byte_count(item, "args_bytes"),+ "output_bytes": _byte_count(item, "result_bytes"),+ "status": status if status in {"ok", "error"} else "unknown",})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:", "failed:", "traceback ", "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.+# Hard per-summary character ceiling layered on top of the dynamic headroom+# budget (see _apply_summary_budget): belt-and-suspenders for models that+# ignore "be concise". 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).+# Fraction of the parent's *remaining* context headroom the whole batch of+# summaries may consume, split per summary, so N children can't collectively+# blow the parent's window (the compression/429 death spiral)._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 = 2000def _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).- """+ """Write the full summary under ``cache/delegation`` (mounted read-only into+ remote backends via ``credential_files._CACHE_DIRS``, so the parent's+ terminal/``read_file`` can page it on any backend). Absolute path, or None on+ failure — the trimmed head+tail is still returned regardless."""try:from hermes_constants import get_hermes_dirimport datetime as _dtcache_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"+ path = cache_dir / f"subagent-summary-{task_index}-{_dt.datetime.now().strftime('%Y%m%d_%H%M%S_%f')}.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.+ # 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:@@ -272,133 +200,96 @@ def _spill_summary_to_file(task_index: int, summary: str) -> Optional[str]:return Nonedef _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.- """+ """``(model_text, spill_path)`` for one over-budget summary: a ~75% head /+ ~25% tail window snapped to line boundaries (so the opening AND the closing+ outcomes/files-changed/issues both survive), the full text spilled to disk,+ and a footer giving the exact ``read_file offset=`` for the omitted middle."""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.+ head, tail = summary[:head_budget], summary[-tail_budget:]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,+ "",+ "─" * 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") + 2footer_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"offset={head.count(chr(10)) + 2} 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+ return head + "\n\n[... middle omitted — see footer ...]\n\n" + tail + "\n".join(footer_lines), spill_pathdef _parent_summary_char_budget(parent_agent, n_summaries: int) -> Optional[int]:"""Per-summary char budget from the parent's *remaining* context headroom- (context length minus prompt tokens minus the compressor's output reserve),- a fraction of it split across the batch at ~4 chars/token. Guards against N- summaries landing at once, not one large summary. None when the parent's- context state is unknown (no compressor / token count) — caller then uses- the static ceiling only."""+ (context length − prompt tokens − the compressor's output reserve), a+ fraction of it split across the batch at ~4 chars/token. None when the+ parent's context state is unknown — caller then uses the static 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)+ headroom_tokens = context_length - int(used_tokens) - int(getattr(compressor, "max_tokens", 0) or 0)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)+ return _MIN_SUMMARY_CHARS # parent already over budget: floor only+ per_summary_tokens = int(headroom_tokens * _SUMMARY_HEADROOM_FRACTION) // max(1, n_summaries)+ return max(_MIN_SUMMARY_CHARS, per_summary_tokens * 4)except Exception:logger.debug("Summary budget computation failed", exc_info=True)return Nonedef _apply_summary_budget(results: List[Dict[str, Any]], parent_agent) -> None:"""Trim subagent summaries in-place so a batch can't overflow the parent's- context window; full text is spilled to disk so nothing is lost.-- Per-summary cap = MIN(dynamic headroom budget: remaining parent context ÷- batch size, static ``delegation.max_summary_chars`` ceiling; 0 = disabled).- Over-cap summaries become a head+tail slice plus a pointer to the spill file.- Without this, fan-out returned N full summaries verbatim, blowing the parent- context and (on rate-limited providers) triggering a compression/429 death spiral.- """+ context window (full text spilled to disk). Per-summary cap = MIN(dynamic+ headroom budget, static ``delegation.max_summary_chars`` ceiling; 0 = disabled);+ over-cap summaries become head+tail plus a pointer to the spill file."""from tools.delegate_tool import _load_configsummaries = [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))+ static_ceiling = int(_load_config().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]+ candidates = [c for c in (static_ceiling, _parent_summary_char_budget(parent_agent, len(summaries))) if c and c > 0]if not candidates:return # both disabled / unknown → leave summaries untouchedcap = 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_textentry["summary_truncated"] = Trueif spill_path:entry["summary_full_path"] = spill_pathlogger.debug(- "[subagent-%s] summary trimmed %d → ~%d chars (spill=%s)", entry.get("task_index", "?"), original_len, cap,+ "[subagent-%s] summary trimmed %d → ~%d chars (spill=%s)", entry.get("task_index", "?"), len(summary), 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):@@ -415,7 +306,7 @@ def _build_child_preserving_parent_tools(**kwargs):return childdef _parent_finalization_lock(parent_agent) -> threading.RLock:- """Return the per-parent lock that serializes lifecycle side effects."""+ """Per-parent lock serializing lifecycle side effects (created once under the guard)."""if parent_agent is None:return _PARENT_FINALIZATION_FALLBACK_LOCKlock = getattr(parent_agent, "_subagent_finalization_lock", None)@@ -453,7 +344,6 @@ def _fire_subagent_stop_hooks(results, child_by_index, parent_agent) -> float:from hermes_cli.plugins import invoke_hook as invoke_hookexcept Exception:invoke_hook = None-children_cost_total = 0.0for entry in results:child_role = entry.pop("_child_role", None)@@ -511,10 +401,5 @@ def _run_child_lifecycle(task_index: int, goal: str, child=None, parent_agent=Noresult = _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,- )+ _finalize_child_results([result], [{"goal": ""} for _ in range(task_index)] + [task], [(task_index, task, child)], parent_agent)return result