mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-05-18 04:41:56 +00:00
chore: ruff auto-fix PLR6201 — tuple → set in membership tests (#23937)
Replace with for all literal-tuple membership tests. Set lookup is O(1) vs O(n) for tuple — consistent micro-optimization across the codebase. 608 instances fixed via `ruff --fix --unsafe-fixes`, 0 remaining. 133 files, +626/-626 (net zero).
This commit is contained in:
parent
8c11710314
commit
2ec8d2b42f
133 changed files with 626 additions and 626 deletions
64
run_agent.py
64
run_agent.py
|
|
@ -539,7 +539,7 @@ def _trajectory_normalize_msg(msg: Dict[str, Any]) -> Dict[str, Any]:
|
|||
if isinstance(content, list):
|
||||
cleaned = []
|
||||
for p in content:
|
||||
if isinstance(p, dict) and p.get("type") in ("image", "image_url", "input_image"):
|
||||
if isinstance(p, dict) and p.get("type") in {"image", "image_url", "input_image"}:
|
||||
cleaned.append({"type": "text", "text": "[screenshot]"})
|
||||
else:
|
||||
cleaned.append(p)
|
||||
|
|
@ -903,7 +903,7 @@ def _strip_images_from_messages(messages: list) -> bool:
|
|||
continue
|
||||
new_parts = []
|
||||
for part in content:
|
||||
if isinstance(part, dict) and part.get("type") in ("image_url", "image", "input_image"):
|
||||
if isinstance(part, dict) and part.get("type") in {"image_url", "image", "input_image"}:
|
||||
found = True
|
||||
else:
|
||||
new_parts.append(part)
|
||||
|
|
@ -1393,7 +1393,7 @@ class AIAgent:
|
|||
|
||||
_pc_cfg = _load_pc_cfg().get("prompt_caching", {}) or {}
|
||||
_ttl = _pc_cfg.get("cache_ttl", "5m")
|
||||
if _ttl in ("5m", "1h"):
|
||||
if _ttl in {"5m", "1h"}:
|
||||
self._cache_ttl = _ttl
|
||||
except Exception:
|
||||
pass
|
||||
|
|
@ -1640,7 +1640,7 @@ class AIAgent:
|
|||
# but no credentials were found, fail fast with a clear
|
||||
# message instead of silently routing through OpenRouter.
|
||||
_explicit = (self.provider or "").strip().lower()
|
||||
if _explicit and _explicit not in ("auto", "openrouter", "custom"):
|
||||
if _explicit and _explicit not in {"auto", "openrouter", "custom"}:
|
||||
# Look up the actual env var name from the provider
|
||||
# config — some providers use non-standard names
|
||||
# (e.g. alibaba → DASHSCOPE_API_KEY, not ALIBABA_API_KEY).
|
||||
|
|
@ -2029,7 +2029,7 @@ class AIAgent:
|
|||
compression_threshold = _model_cthresh
|
||||
except Exception:
|
||||
pass
|
||||
compression_enabled = str(_compression_cfg.get("enabled", True)).lower() in ("true", "1", "yes")
|
||||
compression_enabled = str(_compression_cfg.get("enabled", True)).lower() in {"true", "1", "yes"}
|
||||
compression_target_ratio = float(_compression_cfg.get("target_ratio", 0.20))
|
||||
compression_protect_last = int(_compression_cfg.get("protect_last_n", 20))
|
||||
|
||||
|
|
@ -2543,7 +2543,7 @@ class AIAgent:
|
|||
# tests) can't reintroduce the double-/v1 404 bug.
|
||||
if (
|
||||
api_mode == "anthropic_messages"
|
||||
and new_provider in ("opencode-zen", "opencode-go")
|
||||
and new_provider in {"opencode-zen", "opencode-go"}
|
||||
and isinstance(base_url, str)
|
||||
and base_url
|
||||
):
|
||||
|
|
@ -4280,7 +4280,7 @@ class AIAgent:
|
|||
metadata["task_id"] = task_id
|
||||
if tool_call_id:
|
||||
metadata["tool_call_id"] = tool_call_id
|
||||
return {k: v for k, v in metadata.items() if v not in (None, "")}
|
||||
return {k: v for k, v in metadata.items() if v not in {None, ""}}
|
||||
|
||||
def _apply_persist_user_message_override(self, messages: List[Dict]) -> None:
|
||||
"""Rewrite the current-turn user message before persistence/return.
|
||||
|
|
@ -4494,7 +4494,7 @@ class AIAgent:
|
|||
for p in content:
|
||||
if isinstance(p, dict) and p.get("type") == "text":
|
||||
_txt.append(str(p.get("text", "")))
|
||||
elif isinstance(p, dict) and p.get("type") in ("image", "image_url", "input_image"):
|
||||
elif isinstance(p, dict) and p.get("type") in {"image", "image_url", "input_image"}:
|
||||
_txt.append("[screenshot]")
|
||||
content = "\n".join(_txt) if _txt else None
|
||||
tool_calls_data = None
|
||||
|
|
@ -4853,11 +4853,11 @@ class AIAgent:
|
|||
context["message"] = message.strip()
|
||||
for key in ("resets_at", "reset_at"):
|
||||
value = payload.get(key)
|
||||
if value not in (None, ""):
|
||||
if value not in {None, ""}:
|
||||
context["reset_at"] = value
|
||||
break
|
||||
retry_after = payload.get("retry_after")
|
||||
if retry_after not in (None, "") and "reset_at" not in context:
|
||||
if retry_after not in {None, ""} and "reset_at" not in context:
|
||||
try:
|
||||
context["reset_at"] = time.time() + float(retry_after)
|
||||
except (TypeError, ValueError):
|
||||
|
|
@ -5678,9 +5678,9 @@ class AIAgent:
|
|||
if self.valid_tool_names:
|
||||
_enforce = self._tool_use_enforcement
|
||||
_inject = False
|
||||
if _enforce is True or (isinstance(_enforce, str) and _enforce.lower() in ("true", "always", "yes", "on")):
|
||||
if _enforce is True or (isinstance(_enforce, str) and _enforce.lower() in {"true", "always", "yes", "on"}):
|
||||
_inject = True
|
||||
elif _enforce is False or (isinstance(_enforce, str) and _enforce.lower() in ("false", "never", "no", "off")):
|
||||
elif _enforce is False or (isinstance(_enforce, str) and _enforce.lower() in {"false", "never", "no", "off"}):
|
||||
_inject = False
|
||||
elif isinstance(_enforce, list):
|
||||
model_lower = (self.model or "").lower()
|
||||
|
|
@ -5935,7 +5935,7 @@ class AIAgent:
|
|||
return False
|
||||
continue
|
||||
btype = block.get("type")
|
||||
if btype in ("thinking", "redacted_thinking"):
|
||||
if btype in {"thinking", "redacted_thinking"}:
|
||||
continue
|
||||
if btype == "text":
|
||||
text = block.get("text", "")
|
||||
|
|
@ -6665,7 +6665,7 @@ class AIAgent:
|
|||
if done_item is not None:
|
||||
collected_output_items.append(done_item)
|
||||
# Log non-completed terminal events for diagnostics
|
||||
elif event_type in ("response.incomplete", "response.failed"):
|
||||
elif event_type in {"response.incomplete", "response.failed"}:
|
||||
resp_obj = getattr(event, "response", None)
|
||||
status = getattr(resp_obj, "status", None) if resp_obj else None
|
||||
incomplete_details = getattr(resp_obj, "incomplete_details", None) if resp_obj else None
|
||||
|
|
@ -6767,7 +6767,7 @@ class AIAgent:
|
|||
done_item = event.get("item")
|
||||
if done_item is not None:
|
||||
collected_output_items.append(done_item)
|
||||
elif event_type in ("response.output_text.delta",):
|
||||
elif event_type in {"response.output_text.delta",}:
|
||||
delta = getattr(event, "delta", "")
|
||||
if not delta and isinstance(event, dict):
|
||||
delta = event.get("delta", "")
|
||||
|
|
@ -7063,7 +7063,7 @@ class AIAgent:
|
|||
effective_reason = FailoverReason.billing
|
||||
elif status_code == 429:
|
||||
effective_reason = FailoverReason.rate_limit
|
||||
elif status_code in (401, 403):
|
||||
elif status_code in {401, 403}:
|
||||
effective_reason = FailoverReason.auth
|
||||
|
||||
if effective_reason == FailoverReason.billing:
|
||||
|
|
@ -8384,7 +8384,7 @@ class AIAgent:
|
|||
auth resolution and client construction — no duplicated provider→key
|
||||
mappings.
|
||||
"""
|
||||
if reason in (FailoverReason.rate_limit, FailoverReason.billing):
|
||||
if reason in {FailoverReason.rate_limit, FailoverReason.billing}:
|
||||
# Only start cooldown when leaving the primary provider. If we're
|
||||
# already on a fallback and chain-switching, the primary wasn't the
|
||||
# source of the 429 so the cooldown should not be reset/extended.
|
||||
|
|
@ -8710,7 +8710,7 @@ class AIAgent:
|
|||
if self._is_openrouter_url():
|
||||
return False
|
||||
provider_lower = (self.provider or "").strip().lower()
|
||||
if provider_lower in ("nous", "nous-research"):
|
||||
if provider_lower in {"nous", "nous-research"}:
|
||||
return False
|
||||
|
||||
try:
|
||||
|
|
@ -10304,7 +10304,7 @@ class AIAgent:
|
|||
store=self._memory_store,
|
||||
)
|
||||
# Bridge: notify external memory provider of built-in memory writes
|
||||
if self._memory_manager and function_args.get("action") in ("add", "replace"):
|
||||
if self._memory_manager and function_args.get("action") in {"add", "replace"}:
|
||||
try:
|
||||
self._memory_manager.on_memory_write(
|
||||
function_args.get("action", ""),
|
||||
|
|
@ -10403,7 +10403,7 @@ class AIAgent:
|
|||
function_args = {}
|
||||
|
||||
# Checkpoint for file-mutating tools
|
||||
if function_name in ("write_file", "patch") and self._checkpoint_mgr.enabled:
|
||||
if function_name in {"write_file", "patch"} and self._checkpoint_mgr.enabled:
|
||||
try:
|
||||
file_path = function_args.get("path", "")
|
||||
if file_path:
|
||||
|
|
@ -10860,7 +10860,7 @@ class AIAgent:
|
|||
logging.debug(f"Tool start callback error: {cb_err}")
|
||||
|
||||
# Checkpoint: snapshot working dir before file-mutating tools
|
||||
if not _execution_blocked and function_name in ("write_file", "patch") and self._checkpoint_mgr.enabled:
|
||||
if not _execution_blocked and function_name in {"write_file", "patch"} and self._checkpoint_mgr.enabled:
|
||||
try:
|
||||
file_path = function_args.get("path", "")
|
||||
if file_path:
|
||||
|
|
@ -10932,7 +10932,7 @@ class AIAgent:
|
|||
store=self._memory_store,
|
||||
)
|
||||
# Bridge: notify external memory provider of built-in memory writes
|
||||
if self._memory_manager and function_args.get("action") in ("add", "replace"):
|
||||
if self._memory_manager and function_args.get("action") in {"add", "replace"}:
|
||||
try:
|
||||
self._memory_manager.on_memory_write(
|
||||
function_args.get("action", ""),
|
||||
|
|
@ -12462,9 +12462,9 @@ class AIAgent:
|
|||
_failure_hint = f"upstream gateway timeout (504, {api_duration:.0f}s)"
|
||||
elif _resp_error_code == 429:
|
||||
_failure_hint = f"rate limited by upstream provider (429)"
|
||||
elif _resp_error_code in (500, 502):
|
||||
elif _resp_error_code in {500, 502}:
|
||||
_failure_hint = f"upstream server error ({_resp_error_code}, {api_duration:.0f}s)"
|
||||
elif _resp_error_code in (503, 529):
|
||||
elif _resp_error_code in {503, 529}:
|
||||
_failure_hint = f"upstream provider overloaded ({_resp_error_code})"
|
||||
elif _resp_error_code is not None:
|
||||
_failure_hint = f"upstream error (code {_resp_error_code}, {api_duration:.0f}s)"
|
||||
|
|
@ -12652,7 +12652,7 @@ class AIAgent:
|
|||
"error": _exhaust_error,
|
||||
}
|
||||
|
||||
if self.api_mode in ("chat_completions", "bedrock_converse", "anthropic_messages"):
|
||||
if self.api_mode in {"chat_completions", "bedrock_converse", "anthropic_messages"}:
|
||||
assistant_message = _trunc_msg
|
||||
if assistant_message is not None and not _trunc_has_tool_calls:
|
||||
length_continue_retries += 1
|
||||
|
|
@ -12692,7 +12692,7 @@ class AIAgent:
|
|||
"error": "Response remained truncated after 3 continuation attempts",
|
||||
}
|
||||
|
||||
if self.api_mode in ("chat_completions", "bedrock_converse", "anthropic_messages"):
|
||||
if self.api_mode in {"chat_completions", "bedrock_converse", "anthropic_messages"}:
|
||||
assistant_message = _trunc_msg
|
||||
if assistant_message is not None and _trunc_has_tool_calls:
|
||||
if truncated_tool_call_retries < 1:
|
||||
|
|
@ -13524,10 +13524,10 @@ class AIAgent:
|
|||
# When a fallback model is configured, switch immediately instead
|
||||
# of burning through retries with exponential backoff -- the
|
||||
# primary provider won't recover within the retry window.
|
||||
is_rate_limited = classified.reason in (
|
||||
is_rate_limited = classified.reason in {
|
||||
FailoverReason.rate_limit,
|
||||
FailoverReason.billing,
|
||||
)
|
||||
}
|
||||
if is_rate_limited and self._fallback_index < len(self._fallback_chain):
|
||||
# Don't eagerly fallback if credential pool rotation may
|
||||
# still recover. See _pool_may_recover_from_rate_limit
|
||||
|
|
@ -13852,7 +13852,7 @@ class AIAgent:
|
|||
or (
|
||||
not classified.retryable
|
||||
and not classified.should_compress
|
||||
and classified.reason not in (
|
||||
and classified.reason not in {
|
||||
FailoverReason.rate_limit,
|
||||
FailoverReason.billing,
|
||||
FailoverReason.overloaded,
|
||||
|
|
@ -13860,7 +13860,7 @@ class AIAgent:
|
|||
FailoverReason.payload_too_large,
|
||||
FailoverReason.long_context_tier,
|
||||
FailoverReason.thinking_signature,
|
||||
)
|
||||
}
|
||||
)
|
||||
) and not is_context_length_error
|
||||
|
||||
|
|
@ -15307,9 +15307,9 @@ def main(
|
|||
info = get_toolset_info(name)
|
||||
if info:
|
||||
entry = (name, info)
|
||||
if name in ["web", "terminal", "vision", "creative", "reasoning"]:
|
||||
if name in {"web", "terminal", "vision", "creative", "reasoning"}:
|
||||
basic_toolsets.append(entry)
|
||||
elif name in ["research", "development", "analysis", "content_creation", "full_stack"]:
|
||||
elif name in {"research", "development", "analysis", "content_creation", "full_stack"}:
|
||||
composite_toolsets.append(entry)
|
||||
else:
|
||||
scenario_toolsets.append(entry)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue