fix: guard float() env var casts against ValueError (16 locations) - #32458
Open
annguyenNous wants to merge 1 commit into
Open
fix: guard float() env var casts against ValueError (16 locations)#32458annguyenNous wants to merge 1 commit into
annguyenNous wants to merge 1 commit into
Conversation
float(os.getenv("VAR", "default")) crashes with ValueError when the
env var contains a non-numeric string like "abc". The `or default`
pattern only protects against falsy values (None, ""), not wrong-type
truthy values.
This fixes 16 unguarded float() casts across 8 files:
Core agent (with _safe_float_env helper):
- agent/chat_completion_helpers.py: HERMES_API_TIMEOUT,
HERMES_STREAM_READ_TIMEOUT, HERMES_STREAM_STALE_TIMEOUT
- agent/auxiliary_client.py: HERMES_NOUS_TIMEOUT_SECONDS (2 locations)
- run_agent.py: HERMES_API_TIMEOUT, HERMES_NOUS_TIMEOUT_SECONDS
CLI:
- hermes_cli/auth.py: HERMES_CODEX_REFRESH_TIMEOUT_SECONDS,
HERMES_XAI_REFRESH_TIMEOUT_SECONDS
- hermes_cli/runtime_provider.py: HERMES_NOUS_TIMEOUT_SECONDS (2 locations)
Platform adapters (inline try/except):
- gateway/platforms/telegram.py: HERMES_TELEGRAM_MEDIA_BATCH_DELAY_SECONDS
- gateway/platforms/wecom.py: HERMES_WECOM_TEXT_BATCH_DELAY_SECONDS,
HERMES_WECOM_TEXT_BATCH_SPLIT_DELAY_SECONDS
- plugins/platforms/discord/adapter.py: HERMES_DISCORD_TEXT_BATCH_DELAY_SECONDS,
HERMES_DISCORD_TEXT_BATCH_SPLIT_DELAY_SECONDS
Files with 3+ locations use a _safe_float_env() helper; files with 1-2
locations use inline try/except for minimal diff.
Contributor
|
Thanks for identifying a real malformed-environment failure. Most of this PR's original sites were independently migrated to the canonical helper in Problems
Suggested changes
Automated hermes-sweeper review. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Guards 16 unguarded
float(os.getenv(...))calls across 8 files againstValueErrorwhen env vars contain non-numeric strings.Bug Pattern
The
or defaultpattern (float(os.getenv("X") or 15)) does NOT protect against this —"abc"is truthy, soornever fires.Files Fixed
agent/chat_completion_helpers.py_safe_float_envhelperagent/auxiliary_client.py_safe_float_envhelperrun_agent.py_safe_float_envhelperhermes_cli/auth.py_safe_float_envhelperhermes_cli/runtime_provider.py_safe_float_envhelpergateway/platforms/telegram.pygateway/platforms/wecom.pyplugins/platforms/discord/adapter.pyImpact
A typo in any of these env vars (e.g.,
HERMES_API_TIMEOUT=abc) currently crashes the entire agent/gateway on startup. With this fix, it logs a warning and uses the default value.Test Plan
py_compileHERMES_API_TIMEOUT=abcand verify agent starts with default timeoutHERMES_DISCORD_TEXT_BATCH_DELAY_SECONDS=xyzand verify Discord adapter starts