Files
hermes-agent/agent/tool_result_classification.py
Teknium 28734be0e5 fix(agent): keep status/all-failed-batch failures counted under top-level judging
Follow-up to the cherry-picked top-level-shape rule. Once a JSON object no
longer reaches the substring scan, two failure shapes that only the scan
caught would go silent:

- {"status": "failed"} / {"status": "error"} with no top-level error key
  (pinned by test_real_error_and_failed_markers_still_flag on main).
- A "results" batch where EVERY item carries an error: delegate_task with
  all children interrupted, a connector batch with every call refused.

shape_failure_error in tool_result_classification covers both and is used
by _detect_tool_failure and classify_tool_failure alike. "timeout" stays out
of the failed statuses because process(wait) returns it for a running job.

Audit against 200,000 real tool results from a live state.db: zero results
newly flagged as failed. The results that stop flagging are partial batches
(some items succeeded) and exit-0 execute_code runs whose nested tool_errors
held one failed inner call; all-failed batches still flag.

Tests trimmed to one invariant covering both classifiers (nested error is
data; top-level error, all-failed batch, and plain-text failures still flag).
2026-10-19 03:33:37 -07:00

97 lines
3.9 KiB
Python

"""Shared helpers for classifying tool result payloads."""
from __future__ import annotations
import json
import re
from typing import Any
FILE_MUTATING_TOOL_NAMES = frozenset({"write_file", "patch"})
# Tools whose interrupted/dangling execution is safe to discard because they
# cannot mutate either external state or Hermes session state. Unknown/plugin/
# MCP tools stay effect-capable by default.
NO_EFFECT_TOOL_NAMES = frozenset({
"read_file", "search_files", "session_search", "skill_view", "skills_list",
"web_extract", "web_search", "vision_analyze", "browser_snapshot",
"browser_get_images", "browser_console", "read_terminal",
})
def tool_may_have_side_effect(tool_name: str) -> bool:
return tool_name not in NO_EFFECT_TOOL_NAMES
# Set by a tool that REFUSED a call the harness judged redundant (repeated identical
# read/search). The body still carries ``"error"`` so the model reads it as a stop
# signal, but nothing failed: failure classifiers must not count it, or the cheap
# refusal feeds the streak that fires ``repeated_exact_failure_block``.
GUARDRAIL_REFUSAL_KEY = "guardrail_refusal"
def is_guardrail_refusal(result: Any) -> bool:
"""Return True when ``result`` (JSON string or parsed dict) is a harness refusal."""
data = result
if isinstance(result, str):
try:
data = json.loads(result.strip())
except Exception:
return False
return isinstance(data, dict) and data.get(GUARDRAIL_REFUSAL_KEY) is True
# A quoted "error"/"failed" in a result's head marks it failed UNLESS it is a key whose value is
# empty: success payloads routinely carry ``"error": null`` per item (web_extract) or a
# ``"failed": 0`` count, and a bare substring test flagged every one of them.
_FAILURE_MARKER_RE = re.compile(
r'"(?:error|failed)"(?!\s*:\s*(?:null|false|0|""|\[\]|\{\})\s*(?:[,}\]]|$))', re.IGNORECASE)
def has_failure_marker(result: str) -> bool:
"""Last-resort text test for a failed tool result: a non-empty ``"error"``/``"failed"``
STARTING in the first 500 chars, or a body starting with ``Error``. The value is read past
the cut, so an ``"error": null`` straddling char 500 is not mistaken for a truncated one."""
head = result[:600]
return any(m.start() < 500 for m in _FAILURE_MARKER_RE.finditer(head)) or result.startswith("Error")
# ``timeout`` is deliberately absent: process(wait) returns it for a still-running job.
_FAILED_STATUSES = frozenset({"failed", "error"})
def shape_failure_error(data: dict) -> str | None:
"""Error text when a JSON-object result failed by SHAPE rather than by its top-level
``error``: a ``status`` of failed/error, or a ``results`` batch where EVERY item carries an
error (delegate_task with all children interrupted, a connector batch with every call
refused). None for a clean or partial result. Top-level JSON objects skip the substring
scan, so this is what keeps those failures counted."""
if str(data.get("status", "")).lower() in _FAILED_STATUSES:
return str(data.get("status"))
items = data.get("results")
if not isinstance(items, list) or not items or not all(isinstance(i, dict) for i in items):
return None
errors = [i.get("error") for i in items]
if not all(errors):
return None
first = errors[0]
return str(first.get("message") or first) if isinstance(first, dict) else str(first)
def file_mutation_result_landed(tool_name: str, result: Any) -> bool:
"""Return True when a file mutation result proves the write landed."""
if tool_name not in FILE_MUTATING_TOOL_NAMES or not isinstance(result, str):
return False
try:
data = json.loads(result.strip())
except Exception:
return False
if not isinstance(data, dict) or data.get("error"):
return False
if tool_name == "write_file":
return "bytes_written" in data
if tool_name == "patch":
return data.get("success") is True
return False