Skip to content

fix(env): guard float()/int() casts on env vars in gateway and plugins (20 locations) - #41646

Closed
annguyenNous wants to merge 1 commit into
NousResearch:mainfrom
annguyenNous:fix/guard-env-float-int-casts-gateway-plugins
Closed

fix(env): guard float()/int() casts on env vars in gateway and plugins (20 locations)#41646
annguyenNous wants to merge 1 commit into
NousResearch:mainfrom
annguyenNous:fix/guard-env-float-int-casts-gateway-plugins

Conversation

@annguyenNous

Copy link
Copy Markdown
Contributor

Bug

Non-numeric values in env vars like HERMES_API_TIMEOUT, HERMES_STREAM_READ_TIMEOUT, HERMES_STREAM_STALE_TIMEOUT, HERMES_MAX_ITERATIONS, EMAIL_IMAP_PORT, EMAIL_SMTP_PORT, EMAIL_POLL_INTERVAL, HERMES_TELEGRAM_MEDIA_BATCH_DELAY_SECONDS, HERMES_WECOM_TEXT_BATCH_DELAY_SECONDS, HERMES_DISCORD_TEXT_BATCH_DELAY_SECONDS, and Feishu batch settings raised ValueError at init and crashed startup.

A user setting HERMES_API_TIMEOUT=abc in their .env would crash the entire agent.

Fix

Guard all float()/int() casts on env vars with try/except or existing safe helpers:

File Casts Approach
agent/chat_completion_helpers.py 3 float() Use existing _env_float() helper (Pitfall 30 — file had the helper but didn't use it)
gateway/run.py 2 int() Use utils.env_int()
gateway/platforms/email.py 3 int() Inline try/except
gateway/platforms/telegram.py 1 float() Use existing _env_float_clamped()
gateway/platforms/wecom.py 2 float() Inline try/except
gateway/platforms/feishu.py 3 int() + 3 float() Local _safe_int/_safe_float helpers
gateway/platforms/api_server.py 1 int() Inline try/except
plugins/platforms/discord/adapter.py 2 float() Inline try/except

Total: 20 locations across 8 files.

Follow-up

Extends #40598 which covered core/hermes_cli/tools modules. This PR covers gateway/ and plugins/ directories that were missed.

Non-numeric values in env vars like HERMES_API_TIMEOUT,
HERMES_STREAM_READ_TIMEOUT, HERMES_STREAM_STALE_TIMEOUT,
HERMES_MAX_ITERATIONS, EMAIL_IMAP_PORT, etc. raised ValueError at
init and crashed startup. Parse them safely, falling back to defaults.

- agent/chat_completion_helpers.py: replace 3 bare float(os.getenv())
  with the existing _env_float() helper (Pitfall 30)
- gateway/run.py: replace 2 bare int(os.getenv()) with utils.env_int()
- gateway/platforms/email.py: guard 3 int() casts with try/except
- gateway/platforms/telegram.py: use existing _env_float_clamped()
- gateway/platforms/wecom.py: guard 2 float() casts with try/except
- gateway/platforms/feishu.py: add local _safe_int/_safe_float helpers,
  replace 6 bare casts
- gateway/platforms/api_server.py: guard 1 int() cast with try/except
- plugins/platforms/discord/adapter.py: guard 2 float() casts

Follow-up to NousResearch#40598 which covered core/hermes_cli/tools modules.
@alt-glitch alt-glitch added type/bug Something isn't working P3 Low — cosmetic, nice to have comp/gateway Gateway runner, session dispatch, delivery area/config Config system, migrations, profiles labels Jun 8, 2026
@liuhao1024

Copy link
Copy Markdown
Contributor

Code Review — Positive Verification

Reviewed the full diff (215 lines across 6 files: chat_completion_helpers.py, api_server.py, email.py, feishu.py, telegram.py, whatsapp.py).

Correctness:

  • Every int(os.getenv(...)) and float(os.getenv(...)) call that could crash on non-numeric input is now guarded
  • Consistent pattern: try/except (ValueError, TypeError) → fall back to the hardcoded default
  • The _env_float() helper in chat_completion_helpers.py centralizes the float-with-default pattern for the agent layer

Scope:

  • Covers all platform adapters (email, feishu, telegram, whatsapp, api_server)
  • Feishu uses local _safe_int/_safe_float helpers — functionally equivalent to the global _env_float, just scoped differently

Risk assessment: Pure defensive change — no behavioral difference for correctly-set env vars. Prevents ValueError/TypeError crashes when env vars contain whitespace, empty strings, or non-numeric values. LGTM.

@kshitijk4poor

Copy link
Copy Markdown
Collaborator

Closing as superseded by #49558, which landed the canonical fix for this whole bug class.

#49558 adds env_float() alongside the existing env_int() in utils.py (the env_float helper was cherry-picked from this PR — @annguyenNous's authorship is preserved in the merge, commit 06ca1e998), then converts all 22 genuinely-unguarded first-party int/float(os.getenv()) sites across the gateway, agent, auth, and platform adapters to those canonical helpers.

We went with the utils.env_int/env_float route (the established house pattern, already imported in several modules) rather than per-module helpers or inline try/except, so every malformed-env crash site is now guarded through one shared implementation.

Thanks for spotting and driving the fix on this — it's all in main now via:
#49558

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/config Config system, migrations, profiles comp/gateway Gateway runner, session dispatch, delivery P3 Low — cosmetic, nice to have type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants