Skip to content

fix(logging): attach gateway log after cli init#16229

Merged
teknium1 merged 1 commit into
NousResearch:mainfrom
helix4u:fix/gateway-log-handler-after-cli-init
Apr 27, 2026
Merged

fix(logging): attach gateway log after cli init#16229
teknium1 merged 1 commit into
NousResearch:mainfrom
helix4u:fix/gateway-log-handler-after-cli-init

Conversation

@helix4u

@helix4u helix4u commented Apr 26, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes the gateway.log handler not being attached when logging is initialized in CLI mode before gateway startup.

The normal hermes gateway run path initializes logging early through the CLI entrypoint, then calls setup_logging(mode="gateway") later from gateway/run.py. Before this change, the second call returned immediately when _logging_initialized was already true, so the gateway-specific gateway.log handler was never attached.

This keeps logging setup idempotent, but allows a later gateway-mode call to attach the missing gateway handler. The existing rotating-handler helper already deduplicates by file path, so repeated gateway setup calls do not add duplicate handlers.

Related Issue

Fixes #8404

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)
  • ✅ Tests (adding or improving test coverage)

Changes Made

  • hermes_logging.py: move the idempotency return until after the gateway handler attachment path so setup_logging(mode="gateway") can add gateway.log after earlier CLI setup.
  • tests/test_hermes_logging.py: add regression coverage for CLI → gateway initialization and repeated gateway setup calls.

How to Test

  1. Run hermes_logging.setup_logging(mode="cli").
  2. Run hermes_logging.setup_logging(mode="gateway").
  3. Verify exactly one gateway.log handler exists and receives gateway.* records.

Targeted test:

.venv/bin/python -m pytest tests/test_hermes_logging.py -n 4

Result: 50 passed in 0.80s

Full suite:

.venv/bin/python -m pytest -n 4

Result: 108 failed, 16071 passed, 41 skipped, 210 warnings in 351.86s

The full-suite failures are broad existing repo failures outside this logging change. Examples include API server default port/config expectations, gateway approval/Tirith setup, DingTalk mocked SDK behavior, broad Discord gateway tests, backup/profile restoration, web server schema, tool arg coercion, Tirith marker cleanup, and TUI provider tests. The targeted logging regression file passes.

Checklist

Code

Documentation & Housekeeping

  • I've updated relevant documentation — N/A
  • I've updated cli-config.yaml.example — N/A
  • I've updated CONTRIBUTING.md or AGENTS.md — N/A
  • I've considered cross-platform impact; this is Python logging setup shared by Docker/local gateway startup
  • I've updated tool descriptions/schemas — N/A

Screenshots / Logs

Targeted test output: 50 passed in 0.80s

Full-suite output: 108 failed, 16071 passed, 41 skipped, 210 warnings in 351.86s

@helix4u helix4u marked this pull request as ready for review April 26, 2026 21:27
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/gateway Gateway runner, session dispatch, delivery comp/cli CLI entry point, hermes_cli/, setup wizard labels Apr 26, 2026
@teknium1 teknium1 merged commit 88a85d3 into NousResearch:main Apr 27, 2026
6 of 7 checks passed
teknium1 added a commit that referenced this pull request May 29, 2026
)

External rotation (logrotate, manual `mv gateway.log gateway.log.1`,
another process rotating the file) leaves `_ManagedRotatingFileHandler`'s
open fd pinned to the renamed inode. All subsequent writes go to the
rotated backup instead of the file every operator expects to read,
producing the symptom 'gateway.log frozen mid-write while agent.log
keeps growing with gateway.* records'.

PR #16229 fixed the original CLI->gateway init-order bug (#8404) so the
handler attaches in the first place. This is the sibling fix for what
happens after attach, when something external rotates underneath us.

Adds a WatchedFileHandler-style inode check on emit(): if baseFilename
no longer matches the open stream's (dev,ino), close the stale fd and
reopen at the expected path. doRollover() refreshes the snapshot so our
own rollover isn't misidentified as external.

Five regression tests cover the matrix: external rename, external
unlink, external truncate (must NOT trigger reopen — inode unchanged),
normal doRollover() (must still work), and the end-to-end
Allen-reproduction (rotate + re-call setup_logging).

55/55 tests in tests/test_hermes_logging.py pass; 5972/5972 in
tests/gateway/ pass.
KKT-OPT pushed a commit to KKT-OPT/hermes-agent that referenced this pull request May 31, 2026
…sResearch#34349)

External rotation (logrotate, manual `mv gateway.log gateway.log.1`,
another process rotating the file) leaves `_ManagedRotatingFileHandler`'s
open fd pinned to the renamed inode. All subsequent writes go to the
rotated backup instead of the file every operator expects to read,
producing the symptom 'gateway.log frozen mid-write while agent.log
keeps growing with gateway.* records'.

PR NousResearch#16229 fixed the original CLI->gateway init-order bug (NousResearch#8404) so the
handler attaches in the first place. This is the sibling fix for what
happens after attach, when something external rotates underneath us.

Adds a WatchedFileHandler-style inode check on emit(): if baseFilename
no longer matches the open stream's (dev,ino), close the stale fd and
reopen at the expected path. doRollover() refreshes the snapshot so our
own rollover isn't misidentified as external.

Five regression tests cover the matrix: external rename, external
unlink, external truncate (must NOT trigger reopen — inode unchanged),
normal doRollover() (must still work), and the end-to-end
Allen-reproduction (rotate + re-call setup_logging).

55/55 tests in tests/test_hermes_logging.py pass; 5972/5972 in
tests/gateway/ pass.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/cli CLI entry point, hermes_cli/, setup wizard comp/gateway Gateway runner, session dispatch, delivery P2 Medium — degraded but workaround exists type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

gateway.log is never attached when logging is initialized in CLI mode before gateway startup

3 participants