fix(env): guard malformed numeric env vars across gateway/agent with utils helpers - #49558
Merged
Conversation
Mirrors the existing env_int() helper: returns the default when the variable is unset or non-numeric instead of raising ValueError. Used by the follow-up commit to guard malformed float env vars across the gateway. Salvaged from #48735 (@annguyenNous). The PR's api_server.py change is now redundant — main guards HERMES_MAX_ITERATIONS via _current_max_iterations().
…s helpers Widen the env_float() guard from #48735 across the whole bug class: a non-numeric value (e.g. a stale .env "HERMES_API_TIMEOUT=abc" or a typo'd port) raised an unhandled ValueError and crashed adapter/agent init. Converts 22 genuinely-unguarded first-party int/float(os.getenv()) sites to the canonical utils.env_int / utils.env_float helpers (the established house pattern), instead of duplicating per-module helpers or inline try/except: - gateway/config.py: WECOM_CALLBACK_PORT, BLUEBUBBLES_WEBHOOK_PORT - gateway/platforms/email.py: EMAIL_IMAP/SMTP_PORT, EMAIL_POLL_INTERVAL - gateway/platforms/feishu.py: dedup cache + text/media batch settings - gateway/platforms/wecom.py, discord/adapter.py: text batch delays - gateway/platforms/telegram.py: media batch delay, TELEGRAM_WEBHOOK_PORT - gateway/platforms/whatsapp.py: WHATSAPP_NPM_INSTALL_TIMEOUT - hermes_cli/auth.py: CODEX/XAI refresh timeouts - agent/chat_completion_helpers.py: API/stream read/stale timeouts - run_agent.py, agent/auxiliary_client.py: API + nous timeouts Sites already guarded by try/except or local helpers are left untouched. The HERMES_MAX_ITERATIONS sites are already guarded on main via _current_max_iterations(), so they are not included.
Contributor
🔎 Lint report:
|
| Rule | Count |
|---|---|
unresolved-attribute |
2 |
First entries
run_agent.py:2984: [unresolved-attribute] unresolved-attribute: Object of type `Self@get_credits_spent_micros` has no attribute `_credits_session_start_micros`
tests/run_agent/test_credits_notices_toggle.py:76: [unresolved-attribute] unresolved-attribute: Unresolved attribute `_credits_session_start_micros` on type `AIAgent`
✅ Fixed issues (1):
| Rule | Count |
|---|---|
invalid-assignment |
1 |
First entries
tests/run_agent/test_credits_notices_toggle.py:76: [invalid-assignment] invalid-assignment: Object of type `None` is not assignable to attribute `_credits_session_start_micros` of type `int`
Unchanged: 5884 pre-existing issues carried over.
Diagnostics are surfaced as warnings — this check never fails the build.
Collaborator
|
Related: #48735 (salvaged predecessor — |
This was referenced Jun 20, 2026
Closed
This was referenced Jun 20, 2026
Ari4ka
approved these changes
Jun 20, 2026
waefrebeorn
pushed a commit
to waefrebeorn/slermes
that referenced
this pull request
Jul 2, 2026
…ar-guards-48735
habarmc1223-sudo
pushed a commit
to habarmc1223-sudo/hermes-agent-fluxmem
that referenced
this pull request
Jul 8, 2026
…ar-guards-48735
santhreal
pushed a commit
to santhreal/hermes-agent
that referenced
this pull request
Jul 13, 2026
…ar-guards-48735
This was referenced Jul 14, 2026
Gravezzz
pushed a commit
to Gravezzz/hermes-agent
that referenced
this pull request
Jul 21, 2026
…ar-guards-48735
leewenjie
pushed a commit
to leewenjie/hermes-agent
that referenced
this pull request
Aug 7, 2026
…ar-guards-48735
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
Malformed numeric env vars no longer crash adapter/agent init. A stale
.envwithHERMES_API_TIMEOUT=abc, a typo'dEMAIL_SMTP_PORT, or any non-numeric value previously raised an unhandledValueErrorand took down gateway platform startup. This converts the remaining genuinely-unguardedint/float(os.getenv())casts to the canonicalutils.env_int/utils.env_floathelpers, which return the default on empty/missing/non-numeric input.Salvaged from #48735 (@annguyenNous) and widened to the whole bug class.
Changes
utils.py: addenv_float()helper alongside the existingenv_int()(salvaged from fix(api-server): guard HERMES_MAX_ITERATIONS against malformed env var #48735, authorship preserved).env_int/env_float:gateway/config.py—WECOM_CALLBACK_PORT,BLUEBUBBLES_WEBHOOK_PORTgateway/platforms/email.py—EMAIL_IMAP_PORT,EMAIL_SMTP_PORT,EMAIL_POLL_INTERVALgateway/platforms/feishu.py— dedup cache + text/media batch settingsgateway/platforms/wecom.py,plugins/platforms/discord/adapter.py— text batch delaysgateway/platforms/telegram.py— media batch delay,TELEGRAM_WEBHOOK_PORTgateway/platforms/whatsapp.py—WHATSAPP_NPM_INSTALL_TIMEOUThermes_cli/auth.py— Codex/xAI OAuth refresh timeoutsagent/chat_completion_helpers.py— API / stream-read / stream-stale timeoutsrun_agent.py,agent/auxiliary_client.py— API + Nous credential timeoutsSites already guarded by try/except or local helpers are left untouched.
HERMES_MAX_ITERATIONSis already guarded onmainvia_current_max_iterations(), so it is not re-touched here — #48735'sapi_server.pychange was redundant and dropped.Why the canonical helper (not per-module helpers / inline try-except)
utils.env_int/env_floatis the established house pattern (already imported inchat_completion_helpers.py,runtime_provider.py,kanban_specify.py). Each conversion is a one-line swap that preserves the original default exactly. Config-fallback patterns likeextra.get("webhook_host") or os.getenv(...)are intentionally left unconverted —env_*can't replicate theorfallback.Validation
EMAIL_SMTP_PORT=abc→ EmailAdapter initValueErrorcrash587HERMES_WECOM_TEXT_BATCH_DELAY_SECONDS=abc→ WeComAdapter initValueErrorcrash0.6env_int/env_floatonabc/empty/missingEmailAdapter/WeComAdapterwith malformed env — both fall back to defaults instead of crashing.tests/gateway/test_email.py+test_config.py+test_config_env_bridge_authority.py+test_feishu_bot_admission.py: 201 passed.ruff checkon all changed files: clean.Credit: @annguyenNous (#48735 —
env_floathelper + canonical-helper approach).