Both bot-to-bot DM retry gates (tools/bot_mode_dm.py::_run_local_turn and tui_gateway/methods_bot_relay.py bot_relay.deliver) classified a failed turn from `(proc.stderr or proc.stdout)`. A failed `hermes … -Q` turn prints the provider prose on stdout (it is the turn's final_response) and `session_id: …` on stderr on every run, so the classifier only ever saw the banner: every 429/5xx/context overflow classified `unknown`, the policy-gated re-run never fired, and the relay sender always got `reason: unknown`. Classify from both streams in the order the CLI writes them (tools.bot_failure_reasons.turn_failure_text) at both sites. Classifying alone double-delivers: the failed attempt's turn-start persist already wrote the DM as the Bot Chat's unanswered tail row, and the re-run (a fresh process replaying the same session + payload) appended a second identical copy. The re-run's child env now carries HERMES_RESUME_UNANSWERED_TURN=1 (tools.bot_relay.retry_turn_env); the quiet one-shot consumes it before the turn (hermes_cli.quiet_single_query.adopt_unanswered_turn) and re-stages the identical unanswered tail row as the pending CLI user dict, stamped durable, so _stage_turn_user_message reuses it and the flush writes no second row. Opt-in by design: an identical tail alone cannot tell a re-run from a person's deliberate re-send. Slimmer redo of #105529 by @jonpol01 (same diagnosis and shape; the marker is consumed at the CLI seam that already reads the dispatcher's env instead of inside agent/turn_context.py). Co-authored-by: John Paul Soliva <soliva.johnpaul@icloud.com>
99 lines
4.0 KiB
Python
99 lines
4.0 KiB
Python
"""Typed failure-reason codes for bot turns and relay replies.
|
|
|
|
A closed vocabulary of machine-readable reason codes carried ALONGSIDE the
|
|
free-text ``error`` fields (additive — old consumers keep working). Platform-side
|
|
codes are assigned by the transport/relay layer; agent-side codes are derived
|
|
from raw agent/provider error text via ``classify_agent_error``. Classifier
|
|
precedence is the order of ``_RULES``: auth outranks quota by design — real
|
|
provider 401 bodies (e.g. Anthropic) say "invalid, blocked or out of funds".
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
|
|
# platform-side
|
|
RUNTIME_OFFLINE = "runtime_offline"
|
|
QUEUED_EXPIRED = "queued_expired"
|
|
DELIVERY_TIMEOUT = "delivery_timeout"
|
|
AGENT_BLOCKED = "agent_blocked"
|
|
CANCELLED = "cancelled"
|
|
|
|
# agent-side
|
|
PROVIDER_AUTH_OR_ACCESS = "provider_auth_or_access"
|
|
PROVIDER_QUOTA_LIMIT = "provider_quota_limit"
|
|
PROVIDER_RATE_LIMIT = "provider_rate_limit"
|
|
PROVIDER_SERVER_ERROR = "provider_server_error"
|
|
CONTEXT_OVERFLOW = "context_overflow"
|
|
MISSING_CONFIG = "missing_config"
|
|
MODEL_UNAVAILABLE = "model_unavailable"
|
|
UNKNOWN = "unknown"
|
|
|
|
ALL_REASONS = frozenset({
|
|
RUNTIME_OFFLINE, QUEUED_EXPIRED, DELIVERY_TIMEOUT, AGENT_BLOCKED, CANCELLED,
|
|
PROVIDER_AUTH_OR_ACCESS, PROVIDER_QUOTA_LIMIT, PROVIDER_RATE_LIMIT,
|
|
PROVIDER_SERVER_ERROR, CONTEXT_OVERFLOW, MISSING_CONFIG, MODEL_UNAVAILABLE, UNKNOWN,
|
|
})
|
|
|
|
#: Reasons a supervisor may retry automatically without human intervention.
|
|
AUTO_RETRYABLE = frozenset({RUNTIME_OFFLINE, DELIVERY_TIMEOUT, PROVIDER_RATE_LIMIT, PROVIDER_SERVER_ERROR})
|
|
|
|
|
|
def is_auto_retryable(reason: str) -> bool:
|
|
return reason in AUTO_RETRYABLE
|
|
|
|
|
|
# Retry session policy: a retried bot turn NEVER mints a fresh session. Transient
|
|
# classes resume as-is; context_overflow runs context compression (the one
|
|
# sanctioned context mutation) on the same session first; everything else
|
|
# (auth/quota/config/model/unknown) is never auto-retried — it can't be fixed by
|
|
# a retry and only burns quota.
|
|
# See #93091.
|
|
RETRY_RESUME = "resume"
|
|
RETRY_COMPRESS_THEN_RESUME = "compress_then_resume"
|
|
RETRY_NONE = "none"
|
|
|
|
|
|
def retry_action(reason: str) -> str:
|
|
"""Map a failure reason to the bot-turn retry action (see policy above)."""
|
|
if reason in AUTO_RETRYABLE:
|
|
return RETRY_RESUME
|
|
if reason == CONTEXT_OVERFLOW:
|
|
return RETRY_COMPRESS_THEN_RESUME
|
|
return RETRY_NONE
|
|
|
|
|
|
_STATUS = r"(?:error code:?\s*|status(?:\s*code)?:?\s*|http\s*)"
|
|
|
|
# Ordered (pattern, code) — first match wins.
|
|
_RULES: tuple[tuple[re.Pattern[str], str], ...] = tuple(
|
|
(re.compile(pat, re.IGNORECASE), code)
|
|
for pat, code in (
|
|
(rf"authentication_error|invalid api key|{_STATUS}(?:401|403)\b", PROVIDER_AUTH_OR_ACCESS),
|
|
(rf"{_STATUS}402\b|out of funds|quota|balance", PROVIDER_QUOTA_LIMIT),
|
|
(rf"{_STATUS}429\b|rate.?limit", PROVIDER_RATE_LIMIT),
|
|
(rf"{_STATUS}5\d{{2}}\b|server error|overloaded", PROVIDER_SERVER_ERROR),
|
|
(r"context length|context_overflow|maximum context", CONTEXT_OVERFLOW),
|
|
(r"no llm provider configured|missing config|no access token", MISSING_CONFIG),
|
|
(r"model .*(not found|does not exist)|model_not_found", MODEL_UNAVAILABLE),
|
|
)
|
|
)
|
|
|
|
|
|
def turn_failure_text(stdout: str | None, stderr: str | None) -> str:
|
|
"""The error text of a failed ``hermes … -Q`` delivery turn: both streams, in the order the CLI
|
|
writes them. The provider prose is the turn's final_response and lands on STDOUT; stderr carries
|
|
session bookkeeping (``session_id: …``) on every run, so ``stderr or stdout`` only ever saw the
|
|
banner and every transient failure classified as ``unknown``."""
|
|
return "\n".join(text.strip() for text in (stdout, stderr) if text and text.strip())
|
|
|
|
|
|
def classify_agent_error(text: str) -> str:
|
|
"""Map raw agent/provider error text to a closed reason code (``unknown`` when unmatched/empty)."""
|
|
raw = str(text or "")
|
|
if raw.strip():
|
|
for pattern, code in _RULES:
|
|
if pattern.search(raw):
|
|
return code
|
|
return UNKNOWN
|